Strong and weak typing
The terms "strong" and "weak" are extremely ambiguous. Here are some ways that the terms are used:
Sometimes, "strong" means "static". That's easy enough, but it's better to say "static" instead because most of us agree on its definition.
Sometimes, "strong" means "doesn't convert between data types implicitly". For example, JavaScript allows us to say
"a" - 1
, which we might call "weak typing". But almost all languages provide some level of implicit conversion, allowing automatic integer-to-float conversion like1 - 1.1
. In practice, most people using "strong" in this way have drawn a line between "acceptable" and "unacceptable" conversions. There is no generally accepted line; they're all arbitrary and specific to the person's opinions.Sometimes, "strong" means that there's no way to escape the language's type rules.
Sometimes, "strong" means memory-safe. C is a notable example of a memory-unsafe language. If
xs
is an array of four numbers, C will happily allow code that doesxs[5]
orxs[1000]
, giving whatever value happens to be in the memory addresses after those used to storexs
.
Let's stop here in the name of brevity. Here's where some languages fall on these metrics. As shown, only Haskell is consistently "strong" by all of these definitions. Most languages are ambiguous.
Language | Static? | Implicit Conversions? | Rules Enforced? | Memory-Safe? |
---|---|---|---|---|
C | Strong | Depends | Weak | Weak |
Java | Strong | Depends | Strong | Strong |
Haskell | Strong | Strong | Strong | Strong |
Python | Weak | Depends | Strong | Strong |
JavaScript | Weak | Weak | Weak | Strong |
(An entry of "Depends" in the "Implicit Conversions" column means that the strong/weak distinction depends on which conversions we consider acceptable.)
Often, the terms "strong" and "weak" refer to unspecified combinations of the various definitions above, and other definitions not shown here. All of this confusion renders "strong" and "weak" effectively meaningless. When tempted to use the terms "strong" or "weak", it's better to simply describe the exact, concrete behavior in question. For example, we might say "JavaScript returns a value when we try to add a string to an integer, but Python throws an error". Then, we don't have to spend the effort to carefully agree on one of the many definitions of "strong"; or, worse, end up with an unresolved misunderstanding simply due to terminology.
Most uses of "strong" and "weak" on the web are vague and ill-defined value judgements: they're used to say that a language is "good" or "bad", with the judgement dressed up in technical jargon. As Chris Smith has written:
Strong typing: A type system that I like and feel comfortable with
Weak typing: A type system that worries me, or makes me feel uncomfortable