Max
Max

Reputation: 20004

Nested union types in F#

Is there any way to crate nested union types in F#? Something like this


type MainType =
    | A of
        | AA of int
        | AB of float
    | B of int   

Upvotes: 6

Views: 962

Answers (2)

nlucaroni
nlucaroni

Reputation: 47934

No, you'll have to separate the types(as in kvb's post). I have heard of plans to add polymorphic variance (as in ocaml) to F#, which would allow you to do something similar.

In ocaml,

type mainType =
    | A of [ `AA of int | `AB of float ]
    | B of int   

Upvotes: 2

kvb
kvb

Reputation: 55184

No, I don't think so. The doesn't seem to be much advantage over creating two separate union types like:

type NestedType =
| AA of int
| AB of float

type MainType =
| A of NestedType
| B of int

let mainValue = A (AA 1)

Upvotes: 4

Related Questions