在C#中相同的接口,我可以在一個類中實現通用接口的兩倍,使用兩種不同類型的參數:實現在不同類屬實例
interface IFoo<T> { void Foo(T x); }
class Bar : IFoo<int>, IFoo<float>
{
public void Foo(int x) { }
public void Foo(float y) { }
}
我想這樣做在F#同樣的事情:
type IFoo<'a> = abstract member Foo : 'a -> unit
type Bar() =
interface IFoo<int> with
[<OverloadID("int")>]
member this.Foo x =()
interface IFoo<float> with
[<OverloadID("float")>]
member this.Foo x =()
但它給一個編譯器錯誤:
This type implements or inherits the same interface at different generic instantiations
'IFoo<float>'
and'IFoo<int>'
. This is not permitted in this version of F#.
我找不到任何discussion of this issue在網絡上。這種用法因爲某種原因而皺眉嗎?是否有計劃在即將發佈的F#版本中允許這樣做?
功能計劃在F#4.0 http://fslang.uservoice.com/forums/245727- f-language/suggestions/5663504-allow-to-implement -with-same-interface-at- – foobarcode 2014-10-19 20:01:31
Pull Request可在以下網址找到:https://github.com/Microsoft/visualfsharp/pull/18 – forki23 2015-01-16 11:24:45