Discriminated unions provide support for values that can be one of several named cases, possibly each with different values and types. Discriminated unions are useful for heterogeneous data;
Discriminated unions allow you to build up complex data structures representing a well-defined set of choices. For example, you need to build an implementation of a choice variable, which has two values yes and no. Using the Union's tool, you can design this.
[ attributes ] type [accessibility-modifier] type-name = | case-identifier1 [of [ fieldname1 : ] type1 [ * [ fieldname2 : ] type2 ...] | case-identifier2 [of [fieldname3 : ]type3 [ * [ fieldname4 : ]type4 ...] [ member-list ]
The following example declares a
type Shape = | Rectangle of width : float * length : float | Circle of radius : float | Prism of width : float * float * height : float
The above code declares a discriminated union Shape, which can have values of three cases: Rectangle, Circle, and Prism. Each case has a different set of fields.
You construct objects by providing values for the named and anonymous fields, as shown below.
let rect = Rectangle( width = 6.0, length = 10.0) let circ = Circle (2.5) let prism = Prism(3.75, 4.0, height = 5.0)
Now let's see how to call and set values for these cases.
type Shape = | Rectangle of width : float * length : float | Circle of radius : float | Prism of width : float * float * height : float let compute vall = match vall with | Rectangle (val1, val2) -> val1*val2 | Circle (val1)->val1*val1*3.1415 | Prism (val1, val2, val3)->val1*val2*val3 let rect = compute (Rectangle( width = 6.0, length = 10.0)) let circ = compute (Circle (2.5)) let prism = compute (Prism(3.75, 4.0, height = 5.0)) Console.WriteLine(rect) Console.WriteLine(circ) Console.WriteLine(prism)