# Union Types in F#

Algebraic Data Types, especially Union Types, are easily one of my favourite features of F#. Let’s have a look.

A Union Type is a set of cases where each case may have associated data. Data can be anything from a function, Record Type, Union type, interface, tuples, etc.

Code Snippet
``````type Point = { X: float; Y: float }
type Membership = Silver | Gold
type Union =
| Function of (int -> int -> int)
| Record of Point
| Union of Membership
| SingleType of int
| Tuple of (int * int)
| NoValue
let printUnion union =
match union with
| Function f -> f 10 10 |> printfn "Function Case with value: %i"
| Record r -> r.X |> printfn "Record Case with value: %f"
| Union u -> u |> printfn "Union Case with value: %A"
| SingleType s -> s |> printfn "Single Type Case with value: %i"
| Tuple (t1, t2) -> printfn "Tuple Case with values: (%i, %i)" t1 t2
| NoValue -> printfn "No Value Case"
Function (fun a b -> a + b) |> printUnion
// Function Case with value: 20
Record { X = 10; Y= 20} |> printUnion
// Record Case with value: 10.000000
Union Gold |> printUnion
// Union Case with value: Gold
SingleType 42 |> printUnion
// Single Type Case with value: 42
Tuple (42, 24) |> printUnion
// Tuple Case with values: (42, 24)
NoValue |> printUnion
// "No Value Case"``````