Search code examples
f#recorddiscriminated-union

Immutable fields in discriminated union


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.


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.