Flow-sensitive typing

In programming language theory, flow-sensitive typing (or flow typing) is a type system where the type of a variable is determined by the control flow.

Classically in statically typed languages, a variable is bound to a single type during its lifetime. However, in flow-sensitive typing, a variable's type may change inside the body of a method, while traversing control flow statements. The type is determined by using type inference and type information is carried using algebraic data types.

Example

See the following example in Ceylon which illustrates the concept:

 1 // Object? means the variable "name" is of type Object or else null
 2 void hello(Object? name) {
 3     if (is String name) {
 4         // "name" now has type String in this block
 5         print("Hello, ``name``!");
 6         // and it is possible to call String methods on the variable
 7         print(" String.size is ``name.size``");
 8     }
 9     else if (exists name) {
10         // "name" now has type Object in this block
11         print("Hello, object ``name``!");
12     }
13     else {
14         print("Hello, world!");
15     }
16 }
17 
18           
19 hello(null);
20 hello(1);
21 hello("John Doe");

Which outputs:

Hello, world!
Hello, object 1!
Hello, John Doe!
 String.size is 8

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 up for terser code, easier to read and modify.

It can also help language implementers to provide faster implementations for dynamic languages by statically predicting the type of objects.[1]

Finally, it increases type safety and can prevent problems due to null pointers, labeled by C.A.R. Hoare—the null reference inventor—as "the billion dollar mistake"[2]

Implementations

Whiley, created by David J. Pearce, was the first language to make use of flow-sensitive typing in 2009.[3][4]

Since this introduction, other languages have made use of it, namely Ceylon,[5] Kotlin,[6][7] TypeScript[8] and Facebook Flow.[9]

External references

  1. Lukas Eder (11 December 2014). "The Inconvenient Truth About Dynamic vs. Static Typing". blog.jooq.org. Retrieved 11 March 2016.
  2. Tony Hoare (2009-08-25). "Null References: The Billion Dollar Mistake". InfoQ.com. I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
  3. David J. Pearce (22 September 2010). "On Flow-Sensitive Types in Whiley". whiley.org. Retrieved 11 March 2016.
  4. David J. Pearce (8 April 2012). "Whiley - Flow Typing". whiley.org. Retrieved 11 March 2016.
  5. "Ceylon - Quick introduction - Typesafe null and flow-sensitive typing". ceylon-lang.org. Retrieved 11 March 2016.
  6. "Null Safety". kotlinlang.org. Retrieved 11 March 2016.
  7. "Type Checks and Casts". kotlinlang.org. Retrieved 11 March 2016.
  8. Ryan Cavanaugh (18 November 2014). "TypeScript 1.4 sneak peek: union types, type guards, and more". blogs.msdn.microsoft.com. Retrieved 11 March 2016.
  9. Avik Chaudhuri, Basil Hosmer, Gabriel Levi (18 November 2014). "Flow, a new static type checker for JavaScript". code.facebook.com. Retrieved 11 March 2016.
This article is issued from Wikipedia - version of the 5/22/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.