Question

I know it's possible to add methods and properties to discriminated unions, but can you add an immutable field that has to be set when an instance the union is created, much like the fields in a record?

I guess what I'd like to do is combine a union type and a record type, like this:

type union =
    | OptionOne of int
    | OptionTwo of string
    {
        AFieldUsedForBothCases : string
    }

which isn't a valid declaration.

I know this can be solved by creating a record type:

type record =
    {
        AFieldUsedForBothCases : string
        TheDiscriminatedUnion : union
    }

but I'd like to do something similar to the first example if possible.

Was it helpful?

Solution

No I don't think so, but you can append it to both cases and extract it with a member:

type union =
    | OptionOne of int * string
    | OptionTwo of string * string
    member u.AFieldUsedForBothCases =
        match u with
        | OptionOne (_,s) | OptionTwo(_,s) -> s

In the end you have to specify the additional element in your constructor anyway. Ok, this one will let you retype the common element on every constructor but I think it's not that bad.

OTHER TIPS

I think this is a neater solution

type union=
|a of int
|b of string

type Realtype = string * union

Thanks to type checking, you can force the property setting, and I think it is a little neater than the record solution

I don't think that is possible. Apart form that I think your second code (using records) makes much more sense because DUs is about "either this or that or that ...", now if there is something common in between all those cases then I would keep that common thing out of the DU rather then inside the DU.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top