Flow-sensitive typing
In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow.[src] In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it.[src] However, in flow-sensitive typing, an expression's type may be updated to a more specific type if it follows an operation that validates its type.[src] Validating operations can include type predicates, imperative updates, and control flow.[src]
In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow.[src] In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it.[src] However, in flow-sensitive typing, an expression's type may be updated to a more specific type if it follows an operation that validates its type.[src] Validating operations can include type predicates, imperative updates, and control flow.[src] // Object?[src] means the variable "name" is of type Object or else null void hello(Object?[src] name) { if (is String name) { // "name" now has type String in this block print("Hello, ``name``!"); // and it is possible to call String methods on the variable print(" String.size is ``name.size``"); } else if (exists name) { // "name" now has type Object in this block print("Hello, object ``name``!"); } else { print("Hello, world!"); } } hello(null); hello(1); hello("John Doe"); and which outputs: Hello, world![src] Hello, object 1![src] Hello, John Doe![src] String.size is 8 fun hello(obj: Any) { // A type cast fails if `obj` is not a String obj as String // Since the type cast did not fail, `obj` must be a String![src] val l = obj.length println("'$obj' is a string of length $l") } hello("Mooooo") ==History and Implementations== The history of the idea goes back at least to Tony Hoare's "record class discriminators" from the mid-1960s.[src] However, practical type systems including it as a feature are much more recent.[src] Typed Scheme, a type system for Scheme developed by Sam Tobin-Hochstadt and first published in 2008, was the first type system to include occurrence typing.[src] Its successor, Typed Racket (a dialect of Racket), is also based on occurrence typing.[src] Shortly after Typed Scheme, David J.[src] Pearce independently reinvented flow-typing in Whiley.[src] Typed JavaScript observed that in "scripting" languages, flow-typing depends on more than conditional predicates; it also depends on state and control flow.[src] This style has since been adopted in languages like Ceylon, TypeScript and Facebook Flow.[src] There are also a few languages that don't have union types but do have nullable types, that have a limited form of this feature that only applies to nullable types, such as C#, Kotlin, and Lobster.[src] ==Alternatives== Pattern matching reaches the same goals as flow-sensitive typing, namely reducing verbosity and making up for terser code, easier to read and modify.[src] It achieves this is in a different way, it allows to match the type of a structure, extract data out of it at the same time by declaring new variable.[src] As such, it reduces the ceremony around type casting and value extraction.[src] Pattern matching works best when used in conjunction with algebraic data types because all the cases can be enumerated and statically checked by the compiler.[src] See this example mock for Java: int eval(Node n) { return switch(n) { // try to type cast "Node" into "IntNode", and create the variable "i" of type "int".[src] // If that works, then return the value of "i" case IntNode(int i) -> i; // try to type cast "Node" into "NegNode", and create the variable "n" of type "Node".[src] // If that works, then return the negation of evaluating the "n" node case NegNode(Node n) -> -eval(n); // try to type cast "Node" into "AddNode", and create the variables "left" and "right" of type "Node".[src] // If that works, then return the addition of evaluating the "left" and "right" nodes case AddNode(Node left, Node right) -> eval(left) + eval(right); // try to type cast "Node" into "MulNode", and create the variables "left" and "right" of type "Node".[src] // If that works, then return the multiplication of evaluating the "left" and "right" nodes case MulNode(Node left, Node right) -> eval(left) * eval(right); // no "default" because the compiler knows all the possible cases have been enumerated }; } In a statically typed language, the advantage of pattern matching over flow-sensitive typing is that the type of a variable always stays the same: it does not change depending on control flow.[src] When writing down the pattern to be matched, a new variable is declared that will have the new type.[src] ==References==[src]
34 sentences · 34 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Flow-sensitive typing." encyclopegia.org, 2026, https://encyclopegia.org/wiki/flow-sensitive-typing. Accessed 27 November 2026.
Chicago
"Flow-sensitive typing." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/flow-sensitive-typing.
APA
encyclopegia.org. (2026). Flow-sensitive typing. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/flow-sensitive-typing
Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| publication date | 2008 | Wikipedia |