3

basedoA() を呼び出せるように、適切なインターフェイス タイプ (つまり)にアップキャストしようとすると、解析エラーが発生しAます。base( http://cs.hubfs.net/topic/None/58670 ) が多少特殊であることは認識していますが、これまでのところ、この特定の問題の回避策を見つけることができませんでした。

助言がありますか?

type A =
    abstract member doA : unit -> string

type ConcreteA() =
    interface A with
        member this.doA() = "a"

type ExtA() = 
    inherit ConcreteA()


interface A with
    override this.doA() = "ex" // + (base :> A).doA() -> parse error (unexpected symbol ':>' in expression)

((new ExtA()) :> A).doA() // output: ex

動作する C# に相当するもの:

public interface A
{
    string doA();
}

public class ConcreteA : A {
    public virtual string doA() { return "a"; }
}

public class ExtA : ConcreteA {
    public override string doA() { return "ex" + base.doA(); }
}

new ExtA().doA(); // output: exa
4

1 に答える 1

6

これは、C#に相当します。

type A =
    abstract member doA : unit -> string

type ConcreteA() =
    abstract doA : unit -> string
    default this.doA() = "a"
    interface A with
        member this.doA() = this.doA()

type ExtA() = 
    inherit ConcreteA()
    override this.doA() = "ex" + base.doA()

ExtA().doA() // output: exa

baseスタンドアロンでは使用できません。メンバーアクセスのみに使用できます(したがって、解析エラー)。MSDNの「クラス」の「継承の指定」を参照してください。

于 2013-02-22T21:41:18.073 に答える