好的,C#有Explictit Interface Implementation 我想在F#中做类似的事情。
我有一些接口(和类)
type IState = interface
abstract member Update : IAction-> IState
...
end
type IEnviroment = interface
abstract member Update : IAction-> IEnviroment
...
end
type IBoard =
inherit IState
inherit IEnviroment
abstract member Update : Move -> IBoard
...
[<AbstractClass>]
and Move ()=
abstract member Apply : IBoard -> IBoard
interface IAction with
override this.Cost = 1M
所以我遇到的问题是Update的定义有3次不同。 所以我需要等同于C#的Explictit Interface Implementation, 我想我会在界面中实现它(因为这在F#中是合法的) - 它只包含一些类型转换。
我的理解是F#中的所有接口实现都是explcit,在类中, 但是一旦接口继承自另一个接口,那么你只能(明确地)实现那个接口。 (所以我的董事会课程只是我的董事会)
答案 0 :(得分:3)
我在member this.IState.Update
的实现中尝试了语法IBoard
,但编译器拒绝了它。
我认为spec没有办法做你想做的事。
这是一个解决此类名称冲突的方法,使用抽象类将调用转发到每个接口。
type I1 =
interface
abstract F : unit -> unit
end
type I2 =
interface
abstract F : unit -> unit
end
type II =
interface
inherit I1
inherit I2
abstract F : unit -> unit
end
[<AbstractClass>]
type III() =
abstract F1 : unit -> unit
abstract F2 : unit -> unit
abstract F3 : unit -> unit
interface I1 with
member this.F() = this.F1()
interface I2 with
member this.F() = this.F2()
type Works() =
inherit III()
override this.F1() = printfn "F1"
override this.F2() = printfn "F2"
override this.F3() = printfn "F3"
type Fails() =
interface II with
member this.F() = ()