Using Discriminated Union Labelled Fields

A few weeks ago, I re-discovered labelled fields in discriminated unions. Despite the fact that they look like tuples, they are not.

This is my entry to F# Advent Calendar 2021. Thanks to Sergey Tihon for organising the Advent Calendar each year.

A few weeks ago, I re-discovered labelled fields in discriminated unions:

// Without labels
type Customer =
    | Registered of string * string option * bool
    | Guest of string

// With labels
type Customer =
    | Registered of Name:string * Email:string option * IsEligible:bool
    | Guest of Name:string

I knew that the feature existed but I've usually built specific types, generally records, for each union case, so hadn't really used them in anger before. This isn't a new feature: Field labels in discriminated union case members were introduced in F# 3.1. Despite the fact that they look like tuples, they are not. For example, tuples in F# do not support labels like they do in C#.

In this post, we will look at how to make use of this feature.

Getting Started

We are going to start with a simple business feature:

(*
Feature: Applying a discount

Scenario: Eligible Registered Customers get 10% discount 
when they spend £100 or more

Given the following Registered Customers
|Customer Id|Email          |Is Eligible|
|John       |john@test.org  |true       |
|Mary       |mary@test.org  |true       |
|Richard    |               |false      |
|Alison     |alison@test.org|false      |

When  spends 
Then their order total will be 

Examples:
|Customer Id| Spend | Total |
|Mary       |  99.00|  99.00|
|John       | 100.00|  90.00|
|Richard    | 100.00| 100.00|
|Sarah      | 100.00| 100.00|
*)

We are going to create two functions: One to calculate the totals after discount and one to return the email address of eligible customers. Emails are mandatory for Eligible customers and optional for Registered customers.

Creating Labelled Fields

The type design used in this post is specifically designed for the task of discovering how we can work with labelled fields. We start with a simple discriminated union with two union cases:

type Customer =
    | Registered of Name:string * Email:string option * IsEligible:bool
    | Guest of Name:string

Pattern Matching on Labelled Fields

As they look like tuples, can we deconstruct them in a match expression in the same way without the labels? It turns out that you can:


 
let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (name, email, isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

In this case, I'm only interested in the IsEligible flag, so will wildcards work? Yes they do:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (_, _, isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

Now let's try adding the labels in and get the values like we would with fields on a record type. Sadly, this doesn't work as we get a compiler error:

// Compiler Error
let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (Name = name, Email = email, IsEligible = isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

As I said earlier, they look like tuples but they aren't. The fix turns out to be simple: Replace the comma separators with semi-colons:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (Name = name; Email = email; IsEligible = isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

As we are not using name and email, can we use wildcards to ignore their data? Yes we can:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (Name = _; Email = _; IsEligible = isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

How about wildcards to ignore the fields? This change gives us a compiler error:

// Compiler Error
let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (_; _; IsEligible = isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

Again, the fix turns out to be simple: Remove the fields completely from the pattern match:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (IsEligible = isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

That's better but it would be nice if we could apply a filter directly rather than having to get the value and then test it. We can do this with records and thankfully it is available here too:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (IsEligible = true) when spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

We can also combine the filter and the value getter as shown in the following function where we filter on IsEligible and return the value of the Email field into a local binding:

let tryGetEligibleEmail customer =
    match customer with
    | Registered (IsEligible = true; Email = email) -> Some email 
    | _ -> None

In summary, we use ',' for separating the fields when we don't use the labels in the pattern match and ';' when we do. If we are not interested in a field, don't use it in the match. We can use filters and value getters in the same match.

Creating an Instance of a DU Case

You can create an instance of a union case without specifying the field labels:

// let john = Registered ( "John", Some "john@test.org", true )

Personally, I think it makes more sense to use the labels if you provided them in the first place:

let john = Registered ( Name = "John", Email = Some "john@test.org", IsEligible = true )
let mary = Registered ( Name = "Mary", Email = Some "mary@test.org", IsEligible = true )
let richard = Registered ( Name = "Richard", Email = None, IsEligible = false )
let alison = Registered ( Name = "Alison", Email = Some "alison@test.org", IsEligible = false )
let sarah = Guest ( Name = "Sarah" )

Verifying these the functions with the instances is trivial. Firstly, the calculateOrderTotal function:

let assertJohn = calculateOrderTotal john 100.0M = 90.0M
let assertMary = calculateOrderTotal mary 99.0M = 99.0M
let assertRichard = calculateOrderTotal richard 100.0M = 100.0M
let assertSarah = calculateOrderTotal sarah 100.0M = 100.0M

and then the tryGetEligibleEmail function:

let assertMaryEmail = tryGetEligibleEmail mary = Some "mary@test.org"
let assertRichardEmail = tryGetEligibleEmail richard = None
let assertAlisonEmail = tryGetEligibleEmail alison = None
let assertSarahEmail = tryGetEligibleEmail sarah = None

What Happens If ...

What happens if you decide not to include a label for the Name field?

type Customer =
    | Registered of string * Email:string option * IsEligible:bool
    | Guest of Name:string

The tuple-style pattern match with no labels works fine as does the version with the wildcards:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (name, email, isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (_, _, isEligible) when isEligible && spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

Removing the label from the original version with labels causes a compiler error:

// Compiler Error
let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (name; Email = email; IsEligible = true) when spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

Removing that field and only using labelled fields works correctly:

let calculateOrderTotal customer spend =
    let discount = 
        match customer with
        | Registered (Email = email; IsEligible = true) when spend >= 100M -> spend * 0.1M 
        | _ -> 0M
    spend - discount

You don't have to supply every field with a label if you aren't going to pattern match on it with that label. I like consistency and would either supply labels to all fields or none at all.

Summary

I hope that you found this short post useful. Even if you decide not to use these features, it is still nice to know that they are available to you.
I have written an ebook called Essential Functional-First F#. All of the royalties go to the F# Software Foundation to support their promotion of the F# language and community around the world.

Follow me on Twitter at @ijrussell!

Blog 5/18/22

Introduction to Functional Programming in F#

Dive into functional programming with F# in our introductory series. Learn how to solve real business problems using F#'s functional programming features. This first part covers setting up your environment, basic F# syntax, and implementing a simple use case. Perfect for developers looking to enhance their skills in functional programming.

Blog 3/22/23

Introduction to Functional Programming in F# – Part 9

Explore Active Patterns and Computation Expressions in F#. Enhance code clarity and functionality with these advanced techniques.

Blog 3/22/23

Introduction to Functional Programming in F# – Part 8

Discover Units of Measure and Type Providers in F#. Enhance data management and type safety in your applications with these powerful tools.

Blog 12/22/22

Introduction to Functional Programming in F# – Part 6

Learn error handling in F# with option types. Improve code reliability using F#'s powerful error-handling techniques.

Blog 10/1/22

Introduction to Functional Programming in F# – Part 4

Unlock F# collections and pipelines. Manage data efficiently and streamline your functional programming workflow with these powerful tools.

Blog 9/13/22

Introduction to Functional Programming in F# – Part 2

Explore functions, types, and modules in F#. Enhance your skills with practical examples and insights in this detailed guide.

Blog 9/15/22

Introduction to Functional Programming in F# – Part 3

Dive into F# data structures and pattern matching. Simplify code and enhance functionality with these powerful features.

Blog 8/8/23

Introduction to Functional Programming in F# – Part 12

Explore reflection and meta-programming in F#. Learn how to dynamically manipulate code and enhance flexibility with advanced techniques.

Blog 7/21/20

Understanding F# applicatives and custom operators

In this post, Jonathan Channon, a newcomer to F#, discusses how he learnt about a slightly more advanced functional concept — Applicatives.

Blog 12/22/22

Introduction to Functional Programming in F# – Part 7

Explore LINQ and query expressions in F#. Simplify data manipulation and enhance your functional programming skills with this guide.

Blog 7/12/23

Introduction to Functional Programming in F# – Part 11

Learn type inference and generic functions in F#. Boost efficiency and flexibility in your code with these essential programming concepts.

Blog 11/30/22

Introduction to Partial Function Application in F#

Partial Function Application is one of the core functional programming concepts that everyone should understand as it is widely used in most F# codebases.In this post I will introduce you to the grace and power of partial application. We will start with tupled arguments that most devs will recognise and then move onto curried arguments that allow us to use partial application.

Blog 8/10/23

Machine Learning Pipelines

In this first part, we explain the basics of machine learning pipelines and showcase what they could look like in simple form. Learn about the differences between software development and machine learning as well as which common problems you can tackle with them.

Service

Spend Management Consulting

Our team of spend management experts can help you analyze, optimize and control your IT spend.

Headerbild zu FinOps
Service 5/28/24

FinOps

Gain visibility into your cloud costs and sustainably reduce them by applying best practices from FinOps.

Blog 10/11/22

Introduction to Functional Programming in F# – Part 5

Master F# asynchronous workflows and parallelism. Enhance application performance with advanced functional programming techniques.

Blog 3/11/21

Introduction to Web Programming in F# with Giraffe – Part 2

In this series we are investigating web programming with Giraffe and the Giraffe View Engine plus a few other useful F# libraries.

Blog 3/10/21

Introduction to Web Programming in F# with Giraffe – Part 1

In this series we are investigating web programming with Giraffe and the Giraffe View Engine plus a few other useful F# libraries.

Blog 5/17/23

Introduction to Functional Programming in F# – Part 10

Discover Agents and Mailboxes in F#. Build responsive applications using these powerful concurrency tools in functional programming.

Kompetenz

Sourcing Strategy, Spend Management & Compliance

The right service providers + Costs under control + Ensure vendor compliance ► Together we develop the right strategy

Bleiben Sie mit dem TIMETOACT GROUP Newsletter auf dem Laufenden!