Home » F# Discriminated Unions

F# Discriminated Unions

by Online Tutorials Library

F# Discriminated Unions

It is useful data structure. It helps to store heterogeneous data. Union is used to represent tree data structures. It provides cases and each case consist of heterogeneous data. If value is not present for any case, The case is equivalent to an enumeration case.

F# Creating Discriminated Unions Example

In the above code we have two cases Rectangle and Circle each case has different set of fields. Now let’s see how to call and set values for these cases.

It allows you either using field name at the time of calling or just passing value as we did in the Square case code. If you use field name, you don’t need to maintain order of field. While passing only value you must ensure order of field before passing value.

F# Discriminated Union Example

Output:

Addition = 20  Subtraction = 10  Multiplication = 10  

You may also like