F# – Enums vs Discriminated Unions

Yan Cui

I help clients go faster for less using serverless technologies.

In my previous post on discriminated unions, I presented discriminated unions as an alternative to standard .Net classes to represent hierarchical data structures. However, in terms of data structure, discriminated unions share much more similarities with enums than they do classes – both allow you to define a set of named constants and associate some data with these constants.

The syntaxes for creating enums and discriminated unions in F# are very similar too:

image

Despite their apparent similarities, there are some significant differences between the two:

    • Enums don’t offer a safety guarantee
    • Enums only hold one piece of data
    • Discriminated unions are reference types
    • Enums can be used as bit flags

Now let’s take a closer look at these differences.

Enums don’t offer a safety guarantee

As enums are little more than syntactic sugar over a primitive integral type such as int, there is no guarantee that the value of an enum is valid. For instance, it’s possible to create an instance of an enum type with an integral value that is not associated with one of the named constants:

image

it’s easy to see how bugs can creep in when you mistakenly create enum values that don’t make any sense, especially when you’re working with enum values from external sources. Which is why it’s a good practice to check the enum values with the static Enum.IsDefined method.

Discriminated unions, on the other hand, can only be one of the defined values, any attempts to do otherwise will be met with a swift compiler error!

Enums only hold one piece of data

This one is self evident from the earlier snippet, enums only hold one piece of data but discriminated unions hold a tuple of data.

Discriminated unions are reference types

Enums are value types and instances of an enum type therefore reside on the stack as a few bytes. Discriminated unions, as do all other reference types, reside in the heap (plus a pointer on the stack whilst it’s still referenced) and need to be garbage collected when they are no longer referenced.

The implication of this is such that enums offer significant performance benefits over discriminated unions. Take the following snippet for instance, where I populate two arrays with 10 million items, one with enums and the other discriminated unions.

image

Averaged over three runs, the enum array took 0.048 seconds to finish whilst the discriminated union array took 1.919 seconds!

Enums can be used as bit flags

From MSDN:

You can use an enumeration type to define bit flags, which enables an instance of the enumeration type to store any combination of the values that are defined in the enumerator list.

image


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


5 thoughts on “F# – Enums vs Discriminated Unions”

  1. Should it not be
    OptionA = 1
    OptionB = 2
    OptionC = 4
    OptionC = 8

    Or is it automatically 2^x for enums?

  2. Wuxab – you’re right, well spotted, completely missed that! I’ve updated the last example accordingly.

  3. Pingback: Dart – Emulating F#’s Discriminated Union (i.e. an algebraic data type) | theburningmonk.com

  4. Pingback: F# Enumerations | Daniel Oliver's Blog

  5. Pingback: How F# can help with the pitfalls of C# enumerations

Leave a Comment

Your email address will not be published. Required fields are marked *