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:
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:
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.
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
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.
Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong