When choosing or learning a new programming language, type system should be your first question. How strict is that language when types don’t really match? Will there be a conservative, slow and annoying compiler? Or maybe a fast feedback loop, often resulting in crashes at runtime? And also, is the language runtime trusting you know what you are doing, even if you don’t? Or maybe it’s babysitting you, making it hard to write fast, low-level code? Believe it or not, I just described static, dynamic, weak and strong typing.
Languages with static typing are quite conservative. If you have a variable that’s known to be an integer, compiler will prevent using it in another context. Integer cannot be interpreted as a date, string, or an object. If a function expects a certain type, compiler will not let you to pass a different one. Statically typed languages catch many bugs at compile type, so you can avoid excessive test cases or manual testing. Moreover, types help navigating code, so static typing is preferred in large, complex codebases. On the other hand, when prototyping or writing one-off scripts, static typing is annoying. Examples of statically typed languages are: Java, C, C++, C#, Go, Rust and of course, Haskell. Also, COBOL is statically typed, as well as Solidity, the language of Ethereum.
Now let’s talk about strong vs. weak typing.
Contrary to some beliefs, strong does not equal static.
As a matter of fact, these type systems are independent of each other.
There can be static but weak type system (for example C and C++).
Also dynamically, but strongly typed language is possible (for example Ruby and Python).
OK, so what’s a strongly typed language?
The definition is kind of blurry.
But in general, it’s a language where types are strictly guarded, even at runtime.
For example, Python is dynamically typed, but trying to add string to int yields a type error.
The same goes for Java: even if you somehow hacked around the type system, the runtime will discover it.
Interestingly C# is sometimes referred as weakly typed due to
That’s it for today, thanks for listening, bye!
Example of typing system in Python
Python 3.8.5 (default, Jul 21 2020, 10:48:26) [Clang 11.0.3 (clang-118.104.22.168)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> x = "abc" >>> y = 123 >>> x + y Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str >>> x = 7 >>> x + y 130