我在我的代碼中有幾個域類型用於區分不同類型的字符串,因此編譯器可以阻止我從例如傳遞參數順序錯誤:非可空字符串的F#類型別名
type Foo = string
type Bar = string
let baz (foo : Foo) (bar : Bar) = printfn "%A %A" foo bar
let f : Foo = "foo"
let b : Bar = "bar"
baz f b // this should be OK
baz b f // this shouldn't compile
然而,這目前還不令人滿意地工作,原因有二:
- 我一直沒能想出一個辦法來指定
null
是不是有效的值,所以我不能保證Foo
實例永遠不會是null
。 - 兩個incantantions實際編譯(並運行) - 所以我一無所獲:d
有沒有一種方法來定義類型別名
一)參考/包同一類型,但與彼此不兼容,並且b)不允許null
值,即使基礎類型允許它?
我不知道的防止任何方式'null'作爲編譯時的值。另請參閱[此SO問題](http://stackoverflow.com/questions/42341535/how-to-make-illegal-values-unrepresentable) –