Checks the type rules for a source language.
A type system is a set of rules that defines how values, variables and functions may be used in a given programming languages.
A type checker, checks that these rules are enforced. The moment that type checking can be done differs per type system, but two extremes exist:
- Static type checking: all checking is done before the program is executed.
- Dynamic type checking: all checking is done during execution of the program.
- Hybrid type checking: when possible checks are done before execution, the remaining checks are done during execution.
These different styles of type checking have different trade offs:
- Static typechecking: Pro: most errors are found before execution. Con: more type declarations have to be written by the programmer and in some situations the type systems limits what can be expressed.
- Dynamic checking: Pro: most flexible and expressive. Con: errors can only be found during execution.
- Hybrid (or gradual) type checking: Pro: a reasonable compromise. Con not be as safe as full static typechecking.
- If in Java a variable has been declared as
boolit cannot be added to an integer.
- If in Java a method has three formal parameters, it cannot be called with four actual parameters.
- In Python, a variable can first get a string value assigned and later on an integer value.