Type systems and type inference
In short: A type system checks that values are only used in permitted ways, for example that a number is not divided by text. Type inference lets the compiler work out types itself.
In more detail: Static types are checked before execution (Java, Rust, TypeScript), dynamic ones only at run time (Python, JavaScript). Strong type systems find many errors early. Generics, sum types and traits make them expressive.
In Depth
- Static vs. dynamic; strong vs. weak: Two separate axes.
- Type inference:
let x = 5→int. Basis: Hindley-Milner (ML, Haskell, Rust). - Generics (parametric polymorphism):
List<T>for any T. - Variance: When is
List<Cat>aList<Animal>? Covariance, contravariance. - Algebraic data types: Sums (either-or) and products (and). With pattern matching.
- Option/Maybe: Makes null values visible in the type and prevents null errors.
- Gradual typing: Add types afterwards (TypeScript, Python type hints).
- Types as documentation and refactoring aid.
See also: Lexer, parser and syntax tree, Intermediate representation and compiler optimisation