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

By default, tuples, dis­crim­i­nat­ed unions and records have what is known as struc­tur­al equal­i­ty – i.e. if two instances of these types have the same set of val­ues then they are con­sid­ered equal, just like val­ue types – despite being ref­er­ence types:

Tuples

image

Dis­crim­i­nat­ed Unions

image

Records

image

To facil­i­tate this form of equal­i­ty, the F# com­pil­er auto­mat­i­cal­ly over­rides Object.Equals and Object.GetHashCode meth­ods for you. How­ev­er, there may be times when you want to use ref­er­en­tial equal­i­ty with these types, in which case you just need to add the Ref­er­enceE­qual­i­ty attribute to your type:

image