Introduction to Type systems

Molunorichie
5 min readMar 13, 2022

TYPE SYSTEMS

Most programming languages are distinguished by several characteristics which may include some kind of type system which sets the categories of objects to be worked with and how they will be handled. Type systems are made of different types that can be seen as a set of possible values, which may include different data types each having a specific ruleset of operation.

Simply put, a type system contains a bunch of rules that dictate the type assigned to various entities in a computer program, these entities may be variables, functions, expressions, or modules. The main purpose of these systems would be for reducing errors about the data types, it does this by enforcing proper representation of data across the program.

Taking python as an example, we’ll be considering two of the pre-defined data types, strings, and int. For strings a set of rules are applied, i.e. they can be concatenated, split, and are iterable, etc. On the other hand, some of these operations aren’t possible for integers, although can be added and subtracted, trying to add an int and a string would throw a type error because it’s against the ruleset for the ‘int’ data type.

The example above is a python interactive shell, two variables A and B were assigned different data, with different data types, number 1 as an integer and Richie as a string.

Passing the two variables through the type function, it pops a <type ‘int’> for A and a <type ‘str’> for B each having different sets of rules following their operations.
The attempt to add A and B mathematically threw a type error because this operation isn’t permitted for string data types. Note that this isn’t the case for some programming languages, the type system may vary with each language.

Programming languages can be further classified based on the type checking process in place, this is grouped into two namely;

— Dynamic Type checking
— Static Type checking

Dynamic Type Checking

In dynamically typed languages, they give more freedom to changing a variable’s type throughout the life cycle, examples of dynamically typed languages are Python, Ruby, PHP, Javascript, etc. For these sets of languages, a variable type isn’t strictly enforced when defined i.e when these variables are defined we don’t need to specify the type. For dynamic type checking, the code has to be executed for the type checkers to detect the errors, for instances where you don’t follow the rules pertaining to the applied data type, the error won’t be raised immediately or before runtime.

>>>  name = "Richie"
>>> type(name)
>>> <type 'str'>
>>>
>>> name = 123
>>> type(name)
>>> <type 'int'>

The above code block is an example of a python code, first, a variable name was assigned the value of Richie which is of type string. This same variable was later assigned a different value of another data type int. For these variables the data types weren’t defined during creation, instead, they were inferred, but this isn’t the case for statically typed programming languages.

Static Type Checking

For statically typed programming languages, they have little to no freedom as regards changing the variable’s types so a fixed data type is assigned to each one throughout their cycle, i.e when defining a variable the data type has to be specified. Good examples are Swift, C, C++, Fortran, Scala, and Java.

One good advantage of statically typed languages is that it allows many type errors to be caught during development. This is possible because the type checking process here occurs before runtime (or during compilation) if the ruleset for the particular data type weren’t obeyed the compiler would throw an error. This then results in a more efficient code since the compiler knows the exact data type to be used throughout the code.

var number: Int = 444
var text: String = "Tree"

The block above is an example in Swift code. You’ll notice the difference between this and that of python, here the data type had to be stated first when defining the variable, it wasn’t so in the case of python. Now if we assign a none string value to the int variable number it’ll raise a type error.

text = 444
error: cannot assign a value of type 'Int' to type 'String'

Further classification of programming languages can be done based on the level of Type safety constraint enforced, this can be grouped into two, namely;

— Strong type systems

— Weak type systems.

It’s a common misconception that Dynamic and static type checking is the same as Strong and Weak type systems, a programming language can be Dynamically typed and have a Strong type system. For strong and weak typed systems if focus more on how a language handles the values it operates on, i.e will the language make some assumptions or will it throw an error if the values concerned don’t have matching types

Strong Type Systems

In a strongly typed language, variables are bound to a specific data type, when two or more variables are in an expression the data type has to match up for it to run properly regardless of when type checking starts, i.e the Type safety constraint are higher.

Python interactive shell

The example above is a python code. Two variables A and B of type int were created. You can see that, the attempt to mathematically add them failed and raised a TypeError thus forcing the dev to fix the bug.

Weak Typed Systems

A weak type system wouldn’t throw an error if two data types don’t match during operation, because the variables aren’t bound to one type, they still have a type though, but the type safety constraint isn’t as strict as those of strongly typed systems. Below is an example of a Javascript code.

const a = 10;
const b = "6";
console.log((a + b));

Two variables A and B, one of type integer and the other string, now adding them together won’t raise an error, here Javascript attempts to convert one of the values to be like the other thereby making the integer act like a string and concatenates them like strings. The result, in this case, would be 106 which is of string type.

Conclusion

In conclusion, we can say that a Dynamically typed language doesn’t necessarily point to a weak language, and a statically typed language won’t always have a strong type system.

As strong and weak type systems, we refer more to how strict the Type safety constraints are enforced, and for Dynamic and Static typing we refer to when type checking occurs which may be at run-time — Dynamic typing or at compile time — Static typing.

With that being said, I hope I have done justice to the Introduction to type systems. If you got more questions or corrections feel free to drop them in the comment section.

--

--