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
  • .NET type classifications
  • Composite types
  • Type Location
  • Particularity of F♯ types / .NET types
  • Types with literal values
  • Algebraic data types (ADT)
  • Why Sum and Product terms?
  • Algebraic types vs Composite types
  • Type abbreviation

Was this helpful?

Edit on GitHub
  1. Types

Overview

.NET type classifications

  1. Value types vs reference types

  2. Primitive types vs composite types

  3. Generic types

  4. Types created from literal values

  5. Algebraic types: sum vs product

Composite types

  • Created by combining other types

  • Can be generic (except enum)

Types

Version

Name

Ref. type

Value type

Types .NET

class

āœ…

āŒ

struct, enum

āŒ

āœ…

Specific to C♯

C♯ 3.0

Anonymous type

āœ…

āŒ

C♯ 7.0

Value tuple

āŒ

āœ…

C♯ 9.0

record (class)

āœ…

āŒ

C♯ 10.0

record struct

āŒ

āœ…

Specific to F♯

Tuple, Record, Union

āœ… (default)

āœ… (opt-in)

F♯ 4.6

Anonymous Record

āœ… (default)

āœ… (opt-in)

šŸ‘‰ F♯ type features are stable and mature.

Type Location

  • Top-level : namespace, top-level module (F♯)

  • Nested : class (C♯), module (F♯)

  • Not definable in let bindings, member

In F♯, all type definitions are made with the type keyword → including classes, enums and interfaces! → but tuples don't need a type definition

Particularity of F♯ types / .NET types

Tuple, Record, Union are:

  • Immutable by default

  • Non-nullable by default

  • Equality and structural comparison (except with fields of function type)

  • sealed: cannot be inherited

  • Support deconstruction, with the same syntax than for construction

Types with literal values

Literal values = instances whose type is inferred

  • Primitive types: true (bool) - "abc" (string) - 1.0m (decimal)

  • Tuples C♯ / F♯ : (1, true)

  • Anonymous types C♯ : new { Name = "Joe", Age = 18 }

  • Records F♯ : { Name = "Joe"; Age = 18 }

ā˜ Note :

  • Types must be defined beforehand ā—

  • Exception: tuples and C♯ anonymous types: implicit definition

Algebraic data types (ADT)

Composite types, combining other types by product or sum.

Let's take the types A and B, then we can create:

  • The product type A Ɨ B:

    • Contains 1 component of type A AND 1 of type B.

    • Anonymous or named components

  • Sum type A + B:

    • Contains 1 component of type A OR 1 of type B.

By extension, same for the product/sum types of N types.

Why Sum and Product terms?

It's related to the number of values:

  • bool → 2 values: true and false

  • unit → 1 value ()

  • int → infinite number of values

The number of values in the composed type will be:

  • The sum of numbers for a sum type: N(A) + N(B)

  • The product of numbers for a product type: N(A) * N(B)

Algebraic types vs Composite types

enum

āœ…

āŒ

F♯ Union

āœ…

āŒ

C♯ class (1), interface, struct

āŒ

āœ…

F♯ Record

āŒ

āœ…

F♯ Tuple

āŒ

āœ…

(1) C♯ classes in the broadest sense: → including modern variations like anonymous type, Value tuple and Record

šŸ‘‰ In C♯, only 1 sum type: enum, very limited / union type šŸ“

Type abbreviation

Alias of another type: type [name] = [existingType]

Different use-cases:

// 1. Document code to avoid repetition
type ComplexNumber = float * float
type Addition<'num> = 'num -> 'num -> 'num // šŸ‘ˆ Also works with generics

// 2. Decouple (partially) usage / implementation
//    → Easier to change the implementation (for a stronger type)
type ProductCode = string
type CustomerId = int

āš ļø Deleted at compile time → no type safety → Compiler allows int to be passed instead of CustomerId !

šŸ’” It is also possible to create an alias for a module šŸ“ module [name] = [existingModule]

āš ļø It's NOT possible to create an alias for a namespace (≠ C♯)

PreviousSummaryNextTuples

Last updated 1 month ago

Was this helpful?