如果是的話,你可以舉一個帶無參數和「parameterfull」構造函數的類型的例子。在F#中。是否有可能重載抽象類型的構造函數?
這是你會推薦使用的東西還是F#提供了一些替代更多功能的方式。如果是,你可以舉一個例子嗎?
如果是的話,你可以舉一個帶無參數和「parameterfull」構造函數的類型的例子。在F#中。是否有可能重載抽象類型的構造函數?
這是你會推薦使用的東西還是F#提供了一些替代更多功能的方式。如果是,你可以舉一個例子嗎?
是否這樣?
type MyType(x:int, s:string) =
public new() = MyType(42,"forty-two")
member this.X = x
member this.S = s
let foo = new MyType(1,"one")
let bar = new MyType()
printfn "%d %s" foo.X foo.S
printfn "%d %s" bar.X bar.S
這是典型的做法。具有「最有參數」的構造函數是隱式構造函數,其餘部分是定義爲類中用於調用隱式構造函數的成員的「新」重載。
編輯
在Beta2中有一個關於抽象類的bug。在某些情況下,你可以解決它使用默認參數的隱式構造,一拉
[<AbstractClass>]
type MyType(?cx:int, ?cs:string) =
let x = defaultArg cx 42
let s = defaultArg cs "forty-two"
member this.X = x
member this.S = s
abstract member Foo : int -> int
type YourType(x,s) =
inherit MyType(x,s)
override this.Foo z = z + 1
type TheirType() =
inherit MyType()
override this.Foo z = z + 1
let foo = new YourType(1,"one")
let bar = new TheirType()
printfn "%d %s" foo.X foo.S
printfn "%d %s" bar.X bar.S
+1:「parameterfull」 :) – Juliet 2009-11-06 20:17:41