好的,一个问题,我想在Array函数中使用一组有区别的联合.在下面的代码,我定义类型ResultVari要么是Unknown或浮点值.我还为类型定义了中缀加运算符,仅当两个args都没有时才返回值Unknown.这很好用.
type ResultVari =
| Unknown
| Value of float
static member (+) (a,b) = // add two ResultVari's together
match a,b with
| Value(av),Value(bv) -> Value(av + bv) // only has a value if both args do.
| _ -> Unknown
(* Summation of array of ResultVari, such that if any are unknown then the result is Unknown *)
let example1 = [| Value(4.0); Value(5.5); Value(3.1) |] // summation should be 12.6
let …Run Code Online (Sandbox Code Playgroud)