DEV Community

Cover image for Type Systems in Programming Languages
Zachary Lee
Zachary Lee

Posted on • Originally published at webdeveloper.beehiiv.com on

Type Systems in Programming Languages

This article will introduce those parts that cannot be ignored in various programming languages — the type system.

What is the Type-System?

When learning a programming language, the first few sections of the introductory course usually introduce the various data types of the language, and they are inseparable from the subsequent programming development. This is enough to show the importance of types to a programming language.

Types in programming languages are broadly classified and can be divided into built-in types represented by int and float, and abstract types represented by class and function. The most striking feature that distinguishes these types is that we can only use its specific operations on a specific type.

But what is the Type-System?

It’s really a system focused on managing types, it’s a logical system consisting of a set of rules that assign properties called types to various structures of a computer program, such as variables, expressions, functions, or modules.

What can a Type-System do?

  1. Define the program type to ensure the security of the program.

  2. It can improve the readability of the code and improve the abstraction level of the code, rather than the low-level inefficient implementation.

  3. It is beneficial to compiler optimization. After specifying a type, the compiler can align it with the corresponding byte, thereby generating efficient machine instructions.

etc.

As can be seen from the above points, the type of a variable can determine a specific meaning and purpose, which is extremely beneficial to our programming.

What is the nature of types in a program?

The type system can be said to be a tool, why do I say this?

Because the program ultimately runs machine code. In the world of machine code, there are no types, those instructions just deal with immediate data or memory.

So the type is essentially an abstraction of memory, and different types correspond to different memory layouts and memory allocation strategies.

For more information on memory management check out my previous article:

Memory Management Every Developer Should Know

Explore crucial memory management techniques that can help web developers write better code and enhance application performance.

favicon webdeveloper.beehiiv.com

Static vs. dynamic typing

We often hear this question, but the difference between the two is when the type checking happens.

The static type system determines the types of all variables at compile-time and throws exceptions in case of incorrect usage.

The dynamic type system determines the variable type at runtime, throws an exception if there is an error, and may crash the program without proper handling.

The early type error reporting of the static type system ensures the security of large-scale application development, while the disadvantage of the dynamic type system is that there is no type checking at compile-time, and the program is not safe enough. Only a large number of unit tests can be used to ensure the robustness of the code. But programs that use the dynamic type system are easy to write and don’t take a lot of time to make sure the type is correct.

Strong vs. weak typing

There is no authoritative definition of the difference between strong typing and weak typing. Most of the early discussions on strong typing and weak typing can be summarized as the difference between static typing and dynamic typing.

But the prevailing saying is that strong types tend to not tolerate implicit type conversions, while weak types tend to tolerate implicit type conversions. In this way, a strongly typed language is usually type-safe, that is, it can only access the memory it is authorized to access in the permitted ways.

Common programming languages

I summarize a classification diagram of common programming language types, note that the four areas of the split are partitions, for example, PHP and JS are both dynamically weakly typed.


If you find this helpful, please consider subscribing to my newsletter for more insights on web development. Thank you for reading!

Top comments (0)