A Brief Introduction to Type Systems

Types provide a way to abstract data. 1 isn’t a collection of bytes in memory, it’s an Integer. Without types programmers would still be shuffling bits in memory. Types bundle up those bits and bytes and give a specific kind of collection of bytes a name. As with every system, no one can agree on a single way of working with types.

Static Typing

Languages using a static type system have all of the types checked when a program is compiled. This can make optimization easier for the compiler because types don’t need to be rechecked every time the program is run. C, Haskell, Java, Fortran and Go use static typing. However, most languages aren’t completely statically typed. Usually languages using static types have ways to get around the strict type requirements by using language features like unions and type casting.

Dynamic Typing

Dynamic typing is the opposite of static typing. Languages that are dynamically typed, like Ruby, Javascript and PHP, do their type checking at runtime. Values, like Integers and Strings, have types but variables do not. A variable in a dynamically typed language can store any kind of value. This makes it easier to write running programs, but the result can be less stable since there are less checks for type safety before the program is run. Dynamic types can also make bugs caused by type errors harder to find because types are not checked until the code is run.

Strong and Weak Typing

If a type system is strong type conversion must be done explicitly by the programmer. For example, if the string “5” and the integer 5 were to be added in C++ the result would be an error. The five would need to be casted to an integer to return 10.

A weakly typed language does that type conversion automatically. The previous example would be perfectly valid without explicit type casting in a weakly typed language.

Duck Typing

Languages that use duck typing are more flexible in the way they handle objects. The type of an object doesn’t matter, only the implementation of the class. For example, if a duck typed language were to call duck.speak(); all that would matter is that the duck object has a method called speak. The type of the duck object doesn’t matter, nor does the rest of the class.

The type system is a key factor of any programming language. It determines the entire structure of the language and how code is written with it. Next time you start a project, consider trying one with a different type system.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s