这是F#bug吗?
我有一个类型正在实现这是F#bug吗?,f#,interface,ienumerable,F#,Interface,Ienumerable,我有一个类型正在实现IEnumerable接口,一切正常: open System type Bar() = interface Collections.IEnumerable with member x.GetEnumerator () = null interface Collections.Generic.IEnumerable<int> with member x.GetEnumerator () = null
IEnumerable
接口,一切正常:
open System
type Bar() =
interface Collections.IEnumerable with
member x.GetEnumerator () = null
interface Collections.Generic.IEnumerable<int> with
member x.GetEnumerator () = null
上面的代码生成类型推断错误:
当'a0:null'没有覆盖任何给定虚拟方法的正确类型时,成员'GetEnumerator:unit->'a0
当'a0:null'没有正确数量的方法类型参数时,成员'GetEnumerator:unit->'a0。所需的签名是“GetEnumerator:unit->Collections.Generic.IEnumerator”。
Microsoft(R)F#2.0交互式构建4.0.30319.1
更新更多标准示例:
type IFoo = abstract Bar : obj list
type IFoo<'a> = abstract Bar : 'a list
inherit IFoo
/* ok */
type Foo = interface IFoo with member x.Bar = []
interface IFoo<Foo> with member x.Bar = []
/* fail */
type FooBase = interface IFoo with member x.Bar = []
type FooDerived = interface IFoo<Foo> with member x.Bar = [] // <---
inherit FooBase
/*
error FS0017: The member 'get_Bar : unit -> 'a list' does not
have the correct type to override any given virtual method.
*/
type IFoo=抽象条:对象列表
类型IFoo编译器无法从“null”实现推断正确的类型。试一试
open System
type Foo() =
interface Collections.IEnumerable with
member x.GetEnumerator () = null
type Bar() =
inherit Foo()
interface Collections.Generic.IEnumerable<int> with
member x.GetEnumerator () : Collections.Generic.IEnumerator<int> = null
开放系统
Foo()类型=
接口Collections.IEnumerable与
成员x.GetEnumerator()=null
类型栏()=
继承Foo()
接口Collections.Generic.IEnumerable与
成员x.GetEnumerator():Collections.Generic.IEnumerator=null
更新:
原因是,由Bar
类型实现的GetEnumerator
方法的类型模棱两可,因为IEnumerable这不是一个bug,这只是一个类型推断失败,因为F#可能在派生接口实现声明中实现继承的接口成员:
type IA = abstract A : int
type IB = inherit IA
type IC = inherit IB
type Baz =
interface IC with
member x.A = 1
因此,在我的示例中,我应该明确指定正确的返回类型,因为派生Bar
类型中的member x.GetEnumerator()
可能同时匹配IEnumerable.GetEnumerator()
和IEnumerable.GetEnumerator()可以发布实际代码吗?好的,让我们使用这样一个接口:type FooSure,但这不一样。等效的示例更愿意阅读类型Boo
,最后阅读类型Bar()=interface Foo和成员x.M()=null
。同样,编译器不能推断Boo(比如IEnumerator)。嗯,你是对的,但是第一个例子呢?如何推断出正确的类型?对不起,我收回我在上一次评论中所说的话。如果允许boot,则该示例实际上可以工作问题似乎与IEnumerable
实现IEnumerable
(非泛型)这一事实有关。如果为Bar
类型显式实现两个接口,那么在继承Foo
的情况下,它也可以在没有任何类型注释的情况下工作是的,问题是如果没有显式指定为IEnumerable和IEnumerable,则GetEnumerable的类型是不明确的
type IA = abstract A : int
type IB = inherit IA
type IC = inherit IB
type Baz =
interface IC with
member x.A = 1