考虑以下代码...
type TypeOne () =
member val Name = "" with get, set
type TypeTwo () =
member val Name = "" with get, set
member val Property = 0 with get, set
[<RequireQualifiedAccess>]
type UnionType =
| TypeOne of TypeOne
| TypeTwo of TypeTwo
// this only succeeds because we have defined the union type a
// requiring qualified access. otherwise TypeOne would be inferred
// as the union case, not the type (understandably)...
let t1 = TypeOne ()
// if we want t1 "as" the union type, we have to do the following...
let ut1 = UnionType.TypeOne t1
// the following function returns a result of type UnionType
// but we have to explicitly do this for each type
let makeUnion switch =
match switch with
| true -> UnionType.TypeOne (TypeOne ())
| _ -> UnionType.TypeTwo (TypeTwo ())
正如评论中的那样,似乎无法推断返回结果应该是联合类型,如果我们必须要求对联合类型进行合格的访问,这是非常冗长的(这似乎是令人担忧的错误)。
也没有办法创建一个只接受联合类型并将它们作为联合返回的函数。(在这种情况下,一个函数接受 TypeOne 或 TypeTwo 并返回 UnionType)。或者有吗?是否有更好的方式与受歧视的工会合作?