假设我有以下代码:集合,实现在F#通用接口的对象
type A =
abstract member hi: string
type B() =
interface A with
member self.hi: string = "Hello"
type C() =
interface A with
member self.hi: string = "Yo"
我可以让F#的类型检查满意A
类型和B
的对象列表,只要我明确指定接口类型,ala:
let l: A list = [ B(); C() ]
但是当通用参数输入图片时,我很难过。例如,
type A<'T> =
abstract member thing: 'T
type B() =
interface A<int> with
member self.thing: int = 1
type C() =
interface A<string> with
member self.thing: string = "Yo"
,我尝试使用类似
let l: A<_> list = [B(); C()]
F#似乎想在泛型类型参数固执地填:我已经使用这个模式
error FS0001: The type 'C' is not compatible with the type 'A<int>'
注在具有标准接口的Java中以及具有特征的Scala中,所以我很惊讶我无法在F#中执行此操作。我在这里错过了什么?