liveBook · Manning

Static typing

As explained in Grokking Simplicity

A type system that checks and constrains the types of program expressions before execution.

Static typing checks program types before execution, usually during compilation. It can catch mistakes such as misspelled field names before the code runs, turning an informal assumption into a checked constraint. section 10.6

Static typing is useful when code relies on names or values that must come from a known set:

  • TypeScript can check that field-name strings belong to a known set of valid fields. section 10.6
  • Java can use an Enum type, while Haskell can use a discriminated union; the best choice depends on the language and team. section 10.6
  • Dynamic typing checks types at run time instead, so the static-versus-dynamic choice involves different trade-offs rather than a settled winner. section 10.8

Checking a field name before execution

Suppose a cart update uses the string "quantitty" instead of "quantity". A static type system can restrict the argument to recognized field names and report the typo before the program runs. Without static typing, a run-time check must validate the name each time the function executes. section 10.6

How Grokking Simplicity covers it

How deep it goes
Defines compile-time checking and contrasts it with run-time checking, but does not explain type-system internals.
Examples
Uses field-name strings, TypeScript, Java enums, Haskell discriminated unions, and a JavaScript run-time validation alternative.
What it assumes
Assumes familiarity with program expressions and execution while defining compile-time and run-time checking directly.
What you can do after
Evaluate whether compile-time checks or run-time checks better protect names and types in a chosen language.
How it connects
Connects static typing with compile-time checks, dynamic typing, run-time checks, and functional programming languages.

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 Grokking Simplicity. The section links point to the parts of the book it draws on.