F# Training
F# Training
F# Training
  • Presentation
  • Fundamentals
    • Introduction
    • Syntax
      • Bases
      • Functions
      • Rules
      • Exceptions
    • First concepts
    • πŸ”Quiz
  • Functions
    • Signature
    • Concepts
    • Syntax
    • Standard functions
    • Operators
    • Addendum
    • πŸ”Quiz
    • πŸ“œSummary
  • Types
    • Overview
    • Tuples
    • Records
    • Unions
    • Enums
    • Anonymous records
    • Value types
    • πŸ“œRecap
    • Addendum
  • Monadic types
    • Intro
    • Option type
    • Result type
    • Smart constructor
    • πŸš€Computation expression (CE)
    • πŸš€CE theoretical basements
    • πŸ“œRecap
  • Pattern matching
    • Patterns
    • Match expression
    • Active patterns
    • πŸš€Fold function
    • πŸ“œRecap
    • πŸ•ΉοΈExercises
  • Collections
    • Overview
    • Types
    • Common functions
    • Dedicated functions
    • πŸ”Quiz
    • πŸ“œRecap
  • Asynchronous programming
    • Asynchronous workflow
    • Interop with .NET TPL
    • πŸ“œRecap
  • Module and Namespace
    • Overview
    • Namespace
    • Module
    • πŸ”Quiz
    • πŸ“œRecap
  • Object-oriented
    • Introduction
    • Members
    • Type extensions
    • Class, Struct
    • Interface
    • Object expression
    • Recommendations
Powered by GitBook
On this page
  • Struct tuples & anonymous records
  • Struct records & unions
  • βš–οΈ Pros/Cons

Was this helpful?

Edit on GitHub
  1. Types

Value types

Regular tuple/record/union are reference-types by default, but it's possible to get them as value-types

  • Instances stored on the Stack rather than in the Heap

  • Records, Unions: [<Struct>] attribute

  • Tuples, Anonymous Records: struct keyword

Struct tuples & anonymous records

// Struct tuple
let a = struct (1, 'b', "Three") // struct (int * char * string)

// Struct anonymous record
let b = struct {| Num = 1; Char = 'b'; Text = "Three" |}

Struct records & unions

// Struct record
[<Struct>]
type Point = { X: float; Y: float }
let p = { X = 1.0; Y = 2.3 } // val p: Point = { X = 1.0; Y = 2.3 }

// Struct union: unique fields labels are required❗
[<Struct>]
type Multicase =
    | Int  of i: int
    | Char of c: char
    | Text of s: string
let t = Int 1 // val t: Multicase = Int 1

βš–οΈ Pros/Cons

  • βœ… Efficient because no garbage collection

  • ⚠️ Passed by value β†’ memory pressure

Recommendations:

Consider structs for small types with high allocation rates

PreviousAnonymous recordsNextRecap

Last updated 1 month ago

Was this helpful?

πŸ”—

F# coding conventions / Performance