f-sharp
  1. f-sharp-structures

F# Structures & Unions Structures

F# structures are used to declare user-defined types that consist of a set of related fields. They are used to hold data that is related to each other. Unions structures are also used to declare user-defined data types that can hold one of several possible values.

Syntax

Structures

type <structure_name> =
     { field1 : <type>;
       field2 : <type>;
       ...
       fieldN : <type> } 

Union Structures

type <union_name> =
     | Case1 of <type>
     | Case2 of <type1> * <type2>
     | Case3 of <type>
     ...
     | CaseN of <type>

Example

Structures

type Employee =
    { id : int;
      name : string;
      age : int;
      salary : float }

let emp = { id = 100; name = "John"; age = 25; salary = 5000.0 }
printfn "Id: %d, Name: %s, Age: %d, Salary: %f" emp.id emp.name emp.age emp.salary

Union Structures

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

let findArea shape =
    match shape with
    | Circle(r) -> System.Math.PI * r * r
    | Square(side) -> side * side
    | Rectangle(length, breadth) -> length * breadth

let shape1 = Rectangle(10.0, 20.0)
let area = findArea shape1
printfn "Area of rectangle is %f" area

Output

Structures

Id: 100, Name: John, Age: 25, Salary: 5000.000000

Union Structures

Area of rectangle is 200.000000

Explanation

In F#, structures are used to declare user-defined types that consist of a set of related fields. They are used to hold data that is related to each other. Unions structures are also used to declare user-defined data types that can hold one of several possible values.

The syntax for defining a structure involves declaring a type using the type keyword, giving it a name, and then specifying the fields of the structure using a set of curly braces. Each field is declared by giving it a name, and then specifying its type. The fields are separated by semicolons.

Unions structures are declared by giving the name of the union type, and then specifying one or more "cases" that describe the possible values that the union can hold. Each case is declared using the | symbol, followed by the name of the case, and then followed by the types of the values that the case can hold.

In the example given above, we have declared a structure named Employee, which consists of four fields: id, name, age, and salary. We have also declared a union named Shape, which can hold one of three possible values: Circle, Square, or Rectangle.

Use

Structures and union structures can be used to represent complex data types. They are useful when we have a set of related fields, and we want to group them together into a single type. Union structures are particularly useful when we want to represent data that can take on multiple possible forms.

Important Points

  • Structures and union structures are user-defined data types in F#.
  • Structures are used to group related fields into a single type.
  • Union structures are used to represent data that can take on multiple forms.
  • Structures and union structures are declared using the type keyword.
  • Fields of a structure are enclosed in curly braces and separated by semicolons.
  • Cases of a union structure are separated by |.

Summary

In this tutorial, we learned about F# structures and union structures. Structures are user-defined data types that group related fields into a single type. Union structures are used to represent data that can take on multiple forms. We also looked at example code and learned how to declare structures and union structures, and how to work with them in F# code.

Published on: