F# – Referential equality for tuples, discriminated unions and records

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

MongoDB 8.0 is here to change the game. Faster reads and inserts, and brand-new vector search to support modern AI-powered apps.

Learn More

By default, tuples, discriminated unions and records have what is known as structural equality – i.e. if two instances of these types have the same set of values then they are considered equal, just like value types – despite being reference types:

Tuples

image

Discriminated Unions

image

Records

image

To facilitate this form of equality, the F# compiler automatically overrides Object.Equals and Object.GetHashCode methods for you. However, there may be times when you want to use referential equality with these types, in which case you just need to add the ReferenceEquality attribute to your type:

image

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

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “F# – Referential equality for tuples, discriminated unions and records”

  1. Pingback: Contrasting F# and Elm’s record types | theburningmonk.com

Leave a Comment

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