Girish Mahajan (Editor)

Flow sensitive typing

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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.

Contents

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:

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.

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"

Implementations

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

Since this introduction, other languages have made use of it, namely Ceylon, Kotlin, TypeScript and Facebook Flow.

References

Flow-sensitive typing Wikipedia


Similar Topics