2012-04-04 27 views
2

基本上,我問爲什麼代碼的下列行不會編譯:使用單位的類型參數和壓倒一切的方法

type IGenericType<'a> = 
    abstract member MyFunc : 'a -> 'a -> 'a 

type Implementer() = 
    member x.Test()() =() // unit->unit->unit 
    interface IGenericType<unit> with 
     member x.MyFunc a b = b // FS0017 
     // member x.MyFunc()() =() // FS0017 

只是好奇,如果有,使這項工作按照預期的方法。我認爲這是實施單位和仿製藥的一個限制。

進出口使用以下解決方法的時刻:

type Nothing = 
    | Nothing 
type Implementer() = 
    interface IGenericType<Nothing> with 
     member x.MyFunc a b = b 

希望有人能對此行爲帶來一些光。

回答

2

你猜對了。對於.NET框架內的互操作性,F#編譯器不會爲unit發出IL,而是將其替換爲void

因此,您的通用接口適用於任何類型,但unit。這似乎不是一個大問題;您的解決方法是針對此問題的一個很好的解決方案。

更詳細的討論可參見F# interface inheritance failure due to unit