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 checks how values are used before a program runs. In F#, the compiler and type system catch many potential bugs while code is being written, helping keep concise code safe without requiring a dynamic-language workflow. section 1.2

Types can also carry domain meaning. Rather than treating every value with the same underlying representation as interchangeable, specific types can encode business rules and prevent incorrect data from being mixed together. For example, City and Country may both contain strings but can have separate wrapper types, invariants, and constraints. section 1.5.2

Static typing is useful when code needs to remain clear as it grows:

  • Use the compiler to enforce rules defined for system types and to catch incompatible data before execution. section 1.5.2
  • Model real-world domains with specific, readable types rather than relying only on primitive values. section 1.3
  • Compose small functions with type-system support while retaining syntax close in succinctness to dynamic languages through type inference. section 2.1.2

Specific types protect an address model

An address can distinguish City from Country even when both are represented by text. Giving each its own type makes accidental interchange harder and provides a place to enforce rules for that kind of value. section 1.5.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 a C# method chain with concise F# function composition: static typing and type inference support the F# pipeline without requiring explicit types in the small example. section 2.1.2

Static typing does not limit F# to one application style. It supports scripts and data exploration as well as web services, browser applications compiled to JavaScript, and domain-focused systems. In browser work, it can provide safer F# code while still using JavaScript libraries such as React. section 1.2 section 1.3

The contrast with JavaScript is practical: JavaScript has no compile-time static type checking and can accept questionable operations at run time, which can make debugging and team-scale development harder. Static checking moves more of that feedback earlier, into development and compilation. 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, type inference, and domain wrapper types, but defers detailed inference mechanics.
Examples
Uses even-number filtering and squaring composition, plus City and Country wrapper types in an Address model.
What you can do after
Evaluate/understand how compiler-checked, specific types can encode business rules and prevent incompatible data from being combined.
How it connects
Connects static typing with type inference, type-driven development, domain modeling, function composition, JavaScript, and Fable.

Also explained in

Compare how 12 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.