When to use a Discriminated Union vs Record Type in F#

As my functional programming adventure continues, I come across terms I’m not familiar with. Today, we’ll look at when to use a Discriminated Union vs Record Type.

I strongly believe on the best way to learn something is to teach it to someone else.

It will solidify your knowledge.

(I’m learning F# and before I become too good at it, I’d to share my beginner’s notes with you. Hopefully, it helps you to understand F# and functional programming.)

Discriminated Union

Let’s skip all the boring jargon out there. Here’s the simplest explanation I could find

possible [data] values for which can be enumerated.

Some examples:

type Status =
| Active
| Inactive
| Disabled

type Answer =
| Yes
| No

Answer up above can only be Yes or No.

It can’t be anything else other than those two specific values.

It seems to work similar to C# enums.

Record Type

Skipping all the jargon again:

[used] for complex data which is described by several properties, like a database record or some model entity

These are the equivalent of your classes in a language like C#. But I feel like they are more structs than classes.

Some examples:

type User = { FirstName: string; LastName : string; Age: Int; }

type Cookie = { Name: string; Color: string; }

When to use a Discriminated Union vs Record Type

Think of it as a Record is ‘and’, while a discriminated union is ‘or’.

This is the key.

Record = and
Discriminated Union = or

Example:

type MyRecord = { myString: string
                  myInt: int }

The following value can either be a string OR an int, but not both:

type MyUnion = 
| Int of int
| Str of string

Here’s another discriminated union example in a fictitious game:

type Game =
| Title
| Ingame
| Endgame

You can only be in one area at the time.

You can’t be in the Title screen and be Ingame.
You can’t be in the Ingame and be at Endgame.

Think of this as options for your radio buttons.

Closing Thoughts

If you skipped all the way to the end, make sure you read the When to use a Discriminated Union vs Record Type section at the very least.

Discriminated Union sounds very intimidating. But think of it asL multiple options your value can be.

The simplest comparison is options for your radio buttons.

What do you think? How would you define a discriminated union and a record type in F#?

Resources

  • Eric Olsson

    I really liked your and/or analogy when comparing the different types. In my head, I often picture the difference as contemplating the total number of combinations of values and how adding an element to the type affects that total. With a record type, adding an element has a multiplicative affect on the total whereas adding an element to a discriminated union only has an additive affect.

    • Thanks for your feedback. I prefer using analogies to explain my understanding. Thanks for your comment and stopping by (I admit, I’m still trying to wrap my head around these concepts!)

  • Giorgi Koguashvili
    • Awesome share. Anything by this gentleman is kick ass material!