Static and Dynamic Typing
Static typing checks types before a program runs, while dynamic typing checks them during execution; gradual typing seeks to combine the two within one language.
Definition
Static typing verifies type constraints at compile time so that well-typed programs are accepted before execution; dynamic typing defers type checks to runtime, raising errors when operations are applied to values of inappropriate type.
Scope
This topic covers the spectrum of when and how type checking occurs: static checking that rejects ill-typed programs at compile time, dynamic checking that detects type errors at runtime, and gradual or optional typing that lets the two coexist. It addresses the guarantees, flexibility, and performance implications of each, along with soundness of the type-checking discipline.
Core questions
- What classes of error does static checking prevent that dynamic checking cannot prevent in advance?
- What flexibility does dynamic typing offer, and at what cost?
- How can gradual typing soundly mix typed and untyped code?
- How is the soundness of a static type discipline established?
Key theories
- Gradual typing
- Siek and Taha defined a type system that allows parts of a program to be statically typed and others dynamically typed, with a consistency relation governing safe interaction at the boundary.
- Static type soundness
- Wright and Felleisen's progress-and-preservation method shows that a sound static type system guarantees well-typed programs never reach stuck states, formalizing the safety of static checking.
Clinical relevance
The static-versus-dynamic choice shapes developer workflow, tooling, and reliability. Gradual and optional typing systems, such as those layered onto dynamic languages, let teams add static guarantees incrementally to large existing codebases.
History
Early languages split into statically typed (Algol, Pascal, ML) and dynamically typed (Lisp, Smalltalk) lineages. As dynamic languages grew popular for productivity, researchers sought to reconcile the approaches; Siek and Taha's 2006 gradual typing and contemporaneous work on optional types led to widely used gradually typed systems built on top of dynamic languages.
Debates
- Cost and soundness of gradual typing
- Researchers debate whether sound gradual typing can avoid prohibitive runtime checking costs at typed/untyped boundaries, and whether unsound 'optional' typing is a better practical trade-off.
Key figures
- Benjamin Pierce
- Jeremy Siek
- Walid Taha
- Matthias Felleisen
Related topics
Seminal works
- pierce2002
- siek2006
- wright1994
Frequently asked questions
- Is static typing always better than dynamic typing?
- Neither is universally better; static typing catches more errors early and aids tooling, while dynamic typing offers flexibility and faster prototyping, so the right choice depends on the project's reliability and agility needs.
- What is gradual typing?
- Gradual typing lets a single program mix statically and dynamically typed portions, inserting runtime checks at the boundaries so typed code retains its guarantees while untyped code remains flexible.