Flow-sensitive typing
From Wikipedia, the free encyclopedia
| Type systems |
|---|
| General concepts |
| Major categories |
|
| Minor categories |
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.
In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it. 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. Validating operations can include type predicates, imperative updates, and control flow.
Ceylon
See the following example in Ceylon which illustrates the concept:
// Object? means the variable "name" is of type Object or else null
void hello(Object? 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! Hello, object 1! Hello, John Doe! String.size is 8
Kotlin
See this example in Kotlin:
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!
val l = obj.length
println("'$obj' is a string of length $l")
}
hello("Mooooo")
Benefits
This technique coupled with type inference reduces the need for writing type annotations for all variables or to do type casting, like is seen with dynamic languages that use duck typing. It reduces verbosity and makes for terser code, easier to read and modify.
It can also help language implementers provide implementations that execute dynamic languages faster by predicting the type of objects statically.[1]
Finally, it increases type safety and can prevent problems due to null pointers[how?], labeled by C.A.R. Hoare—the null reference inventor—as "the billion dollar mistake"[2]
From a Programming Languages perspective, it's reasonable to say that flow-sensitive typing is the feature that finally made it possible to build usable type-safe programming languages with union types and without rampant dynamic checking. Until this point, attempts to add this feature to languages such as Scheme generally resulted in intractably large type representations. One example of a system with limited support for union types is Wright and Cartwright's "Soft Scheme."[3]