The concepts of strong and weak typing lack universally accepted definitions, leading to diverse interpretations. A common misunderstanding is to associate the type declarations with strong typing. This is despite the fact that compilers in languages like Go or Rust can infer types, thus not requiring explicity declarations by the programmer, which contradicts the initial association. And optional type annotations are often mistakenly linked with weak typing. Moreover, type declarations are sometimes incorrectly associated with static typing, and type annotations with dynamic typing.
It goes beyond that, but static typing involves type checking at compile-time, whereas dynamic typing involves type checking at runtime.
Regarding the spectrum of strong and weak typing, I tend to define it based on how rigidly a language adheres to type constraints during operations.
Strongly Typed Languages
- In strongly typed languages, a variable, once declared with a specific data type, cannot be assumed to be another type without an explicit conversion.
- Operations between incompatible types are usually disallowed without an explicit conversion.
Example in Python
Example in Go
Weakly Typed Languages
- Weakly typed languages allow variables to be used as though they were of different types with no explicit conversion.
- The language tries to deduce the operation contextually, which can lead to unpredictable results.