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:

⚠️ 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β™―)

Last updated

Was this helpful?