liveBook · Manning

Static typing

As explained in F# in Action: A Revised Edition of Get Programming with F#

A static type system assigns and checks types during compilation rather than primarily during program execution.

Static typing lets the compiler check how values are used before a program runs. In F#, the compiler can flag many potential mistakes as you write, while type inference often removes the need to spell out types for every function. The aim is safety without giving up concise code. section 1.2 section 2.1.2

IEnumerable<int> SquareNumbers(IEnumerable<int> numbers) {
    // implementation of square elided...
}
 
IEnumerable<int> GetEvenNumbersThenSquare(IEnumerable<int> numbers) {
    return SquareNumbers(GetEvenNumbers(numbers))                     #1
}
 
let squareNumbers = Seq.map (fun x -> x * x)
let getEvenNumbersThenSquare = getEvenNumbers >> squareNumbers        #2

Compare the explicit types in the C# methods with the shorter F# functions for selecting even numbers and squaring them. The F# code is still statically typed: type inference reduces the annotations needed to compose the functions. section 2.1.2

Inference does not prevent you from defining precise types where they matter. Suppose an address has a City and a Country. Both could be stored as strings, but separate wrapper types can express their different roles and constraints. The compiler can then help prevent one from being used where the other belongs. This makes business rules visible in the model rather than leaving every distinction to convention. section 1.5.2

This distinction is useful beyond .NET applications. F# code can also be compiled to JavaScript through Fable for browser applications, retaining the benefits of static typing while using JavaScript libraries. JavaScript itself has no compile-time static type checking; that contrast is one reason to consider a typed language for such work. section 1.3 section 11.4.1

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Defines compiler-enforced types and illustrates inference through function composition, but defers a fuller explanation of inference and composition.
Examples
Contrasts C# and F# even-number functions and uses distinct City and Country types to illustrate safer address modeling.
What it assumes
Assumes familiarity with functions and common programming languages, while introducing F# type inference without requiring prior F# knowledge.
What you can do after
Apply it via the F# compiler to model distinct domain values and catch mismatched uses while writing code.
How it connects
Connects type inference, function composition, domain modeling, and JavaScript interoperability to static typing.

Also explained in

Compare how 13 books explain static typing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.