Overview
.NET type classifications
Value types vs reference types
Primitive types vs composite types
Generic types
Types created from literal values
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-levelmodule
(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 inheritedSupport 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 typeB
.Anonymous or named components
Sum type
A + B
:Contains 1 component of type
A
OR 1 of typeB
.
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
andfalse
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?