Strong VS weak typing

Strong VS weak typing

Strongly typed languages possesses grace.

Adeoti Ayodeji's photo
Adeoti Ayodeji
·Nov 3, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Strong typing
  • Weak typing
  • However...

While static and dynamic typing is usually about when types are known, strong and weak typing is about how strict a language is with types.

Strong typing

Languages that are strongly typed take types seriously and will not allow arbitrary manipulation of types as you will. Python, for example will raise an exception if you decide to carry out an operation like so:

string_and_int_operation = "3" + 3
print(string_and_int_operation)

with a TypeError as exception type. Python will not attempt to "make things work" by performing some unintended action.

This behavior is prevalent in most statically typed languages and is helpful to prevent unintended consequences as a result of operations between different types.

Weak typing

In weak typing on the other hand, the interpreter will carry out the operation regardless and yield a result. Whether the result is semantically correct or not is variable.

You'll probably have this "aha" moment if you write JavaScript. While most see it as a weird behavior, it is simply the effects of a weakly typed language. With this, the probability of a program inadvertently crashing due to mismatched types are reduced (I did not say this is a good thing).

Weakly typed languages do not take types very seriously and would coerce types whenever it can. Examples are seen in snippets of JavaScript below:

> "22" + 3 // will result in "223"
> "22" - 3 // will result in 19

However...

The concept of strongly and weakly typed languages are not exactly agreed upon by the programming community as it is a relative matter. At some point, even strongly typed languages like Python makes this operation a legal one:

>>> "Hello " * 3
Hello Hello Hello

You'd rather compare languages in terms of static or dynamic typing rather than strong and weak, since it is what you're likely to be talking about to someone else.

That said, evil languages like Haskell can be considered as strongly typed, while FORTH is said to have no types.

 
Share this