具有基本类型的 F# 元组

F# Tuples with a base type

是否可以同时使用元组和 class 层次结构?

我无法获得接受抽象类型元组参数的函数:

[<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()

您注释的序列中的元组并非来自相同的碱基 class。 (XBase * string) 类型的元组不是元组 class (X * string)(Y * string) 的基础 class 所以这两种不同类型的具体实例不能放在一起.我认为对于任何 .NET 语言(C#,VB)也是如此。

所以你不能创建这个序列:

let tuples = [(new X(), ""); (new Y(), "")]

但是您可以使用以下 2 个序列调用您的 f1 函数:

f1 [(new X(), ""); (new X(), "")]
f1 [(new Y(), ""); (new Y(), "")]