f-sharp
  1. f-sharp-discriminated-unions

F# Structures & Unions: Discriminated Unions

Discriminated unions are a powerful feature of F# that allow you to model real-world concepts as a set of related cases. In this page, we will discuss how to define and use discriminated unions in F#.

Syntax

A discriminated union is defined using the type keyword and the | symbol to separate each case. Each case can have associated data types, separated by the of keyword.

Here's the syntax for defining a discriminated union:

type unionName =
    | case1
    | case2 of datatype
    | case3 of datatype * datatype

Example

Here's an example of defining and using a discriminated union for representing different shapes:

type Shape =
    | Rectangle of float * float
    | Circle of float

let getArea (shape: Shape) =
    match shape with
    | Rectangle (width, height) -> width * height
    | Circle radius -> Math.PI * radius ** 2.0

let myRectangle = Rectangle (5.0, 10.0)
let myCircle = Circle 5.0

printfn "Area of rectangle: %f" (getArea myRectangle)
printfn "Area of circle: %f" (getArea myCircle)

In this example, we define the Shape data type as a discriminated union with two cases: Rectangle and Circle. The Rectangle case has two associated data types, float values representing the width and height of the rectangle. The Circle case has one associated data type, a float value representing the radius of the circle.

We then define a function getArea that takes a Shape as its argument and uses pattern matching to calculate the area of the shape based on its type.

Finally, we create instances of myRectangle and myCircle and pass them to the getArea function to calculate their areas.

Output

When you run the code, it will output:

Area of rectangle: 50.000000
Area of circle: 78.539816

Explanation

Discriminated unions allow you to define types with a fixed set of cases, each with its own associated data types. You can use pattern matching to operate on the different cases within a type. Discriminated unions are a powerful way to model real-world concepts and data structures.

Use

Discriminated unions are useful for representing real-world concepts, such as different shapes, statuses, or options. They allow you to define a set of related cases that can be easily pattern matched on, making it easier to reason about and manipulate your data.

Important Points

  • Discriminated unions are defined using the type keyword and the | symbol to separate cases.
  • Each case can have associated data types, separated by the of keyword.
  • Pattern matching can be used to operate on the different cases within a discriminated union.

Summary

In this page, we discussed how to define and use discriminated unions in F#. We covered the syntax, example, output, explanation, use, important points, and summary of discriminated unions. Discriminated unions are a powerful way of modeling real-world concepts and data structures, making it easier to reason about and manipulate your data.

Published on: