2013-02-22 33 views
3

當我想將base轉發到合適的接口類型(即A),以便我可以調用doA()時,出現解析錯誤。我知道basehttp://cs.hubfs.net/topic/None/58670)有點特別,但到目前爲止我還沒有找到解決這個問題的方法。F#上傳基地

有什麼建議嗎?

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 
+0

在F#中我認爲'override ...'應該放在'ExtA'下,而不是接口。 – 2013-02-22 21:36:56

回答

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不能用於獨立,只爲成員訪問(因此解析錯誤)。請參閱指定繼承,在Classes on MSDN下。

+0

很好的答案,但是你的評論有錯誤的結果... – kvb 2013-02-22 21:43:09

+0

謝謝。它是固定的。 – Daniel 2013-02-22 21:44:07