Mailing list
If you like this book, please consider spreading the word or buying me a coffee
In this chapter you will learn what is a type system and the difference between static and dynamic type systems.
A type system assigns types to programming constructs. By programming constructs we mean functions, expressions, statements and variables among others. The main function of the type system is to remove bugs in software through a phase known as type checking. During type checking the compiler assigns types to language constructs and builds a sound mathematical model. Sound means that the type system guarantees rejects programs that are illegal according to the mathematical model [1].
Type checking can happen at compile time or at run-time; when the type checking happens at compile time, we have a statically typed language. If the type checking happens at run-time, we have a dynamically typed language. For all purposes in this book, we consider only static and dynamic type systems [2].
For example, Java is a statically typed language and would reject the program below when the method readingString
receives an int
argument (e.g. reader.readingString(34)
). The method expects a String
type, not int
type.
On the other side, we have dynamic languages such as Python. Most dynamic languages use duck typing. From the pragmatic point of view, duck typing means that we do not rely on types to specify whether the methods of an object exists: you call methods on objects as if they exist and, at run-time, if they do, great, if they don’t, the program throws an error. You can look at it from this other point of view: you are telling the object the behaviour it should implement, and you must ensure this “contract”. Going back to the previous example, now written in Python:
Let’s assume we have a variable named reader
which is an instance of the Reader
class. You could make the method call reader.readingString("Test")
and get back a singleton list. Moreover, you could also call reader.readingString(True)
and the program would crash at run-time — True
does not have a method called toList()
.
The following table shows the main and most pragmatic differences between static and dynamic languages.
Static | Dynamic |
---|---|
|
|
For more information on these mathematical models, subscribe to the Lambda Calculus for the Working Programmer book.↩
In the research literature, there are languages that mix static and dynamic type systems but these are outside the scope of the book.↩