2

OK、C#はExplictit Interface Implementation です。F#で同様のことをしたいと思います。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 

だから私は持っている問題は、アップデートが異なっ3回定義されていることです。 だから私はC#のequivelentを必要としますExplictit Interface Implementation、 私はインターフェイスで実装していると思っています(これはF#の正当なものです)。これは単に型キャストで構成されます。

私の理解では、F#のすべてのインターフェイス実装は、クラスではexplcitです。 しかし、インターフェイスが別のインターフェイスから継承すると、(明示的に)そのインターフェイスが実装されます。 (私のボードクラスだけが私のボードを暗示する)

+1

名前を変更できませんか? 1つのインターフェースで異なることをする同じ名前の3つのメソッドを持つのは混乱していると思います。そしてあなたの問題も解決します。 – svick

+0

実際には、それらはすべて同じことをします(同じオブジェクトでさえ)。 インターフェイスが提供する/必要とするものだけが異なっています。 彼らは別の目的のために同じことをする –

答えて

3

の実装ではmember this.IState.Updateの構文を試しましたが、コンパイラはそれを拒否しました。

私は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() =() 
+0

thatwas私は何を考えていた –

関連する問題