次のコードを検討してください...
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を返す関数)。それともありますか?差別化された組合と協力するためのより良い方法はありますか?