我想要一個空構造函數和一個接受參數並將其分配給公共屬性的構造函數重載。f#中帶有屬性賦值的多個構造函數
這是我堅持:
type TemplateService() =
interface ITemplateService with
//Properties
member TemplateDirectory = ""
//Constructors
new (templateDirectory:string) = //Error here.
if (templateDirectory == null) then
raise (new System.ArgumentNullException("templateDirectory"))
TemplateDirectory = templateDirectory;
它給我的錯誤:在對象表達`意外的關鍵詞「新」。預期的「成員」,「覆蓋」或其他標記。
如果我使用member
,物業TemplateDirectory
給出了這樣的錯誤:
This instance member needs a parameter to represent the object being invoked. Make the member static or use the notation 'member x.Member(args) = ...'
如果我想在分配前檢查'd'是空還是空,該怎麼辦?比如在它之前拋出一個錯誤。 – 2011-03-15 03:58:20
請參閱Daniel編輯的版本進行空檢查。 – 2011-03-15 04:51:26
但我只需要爲接受爭論的構造函數做這個測試。如果我做'新的TemplateService()',那麼它會拋出一個錯誤。 – 2011-03-15 05:04:14