Reputation:
Is it possible to use tuples and class hierarchies together?
I cannot get a function to accept a tuple parameter which is an abstract type:
[<AbstractClass>]
type XBase () =
member val Name="" with get, set
type X () =
inherit XBase ()
type Y () =
inherit XBase ()
//fine without tuples
let f(values:seq<XBase>) =
printf "ok."
f [new X(); new Y()]
//this function won't work
let f1<'V when 'V :> XBase>(keysAndValues:seq<'V * string>) =
printf "ok."
//compiler error: This expression was expected to have type X but here it has type Y
//f1 [(new X(), ""); (new Y(), "")]
System.Console.ReadLine()
Upvotes: 1
Views: 136
Reputation: 4280
Tuples in your commented sequence are not derived from the same base class. Tuple of type (XBase * string)
is not a base class for tuple class (X * string)
or (Y * string)
So these concrete instances of 2 different types cannot be put in the collection together. I think it's true for any .NET language (C#, VB) also.
So you cannot create this sequence:
let tuples = [(new X(), ""); (new Y(), "")]
But you can invoke your f1 functions with these 2 sequences:
f1 [(new X(), ""); (new X(), "")]
f1 [(new Y(), ""); (new Y(), "")]
Upvotes: 4