3 ms·
Lots of people don't realize that strong/weak typing is orthogonal to static/dynamic typing. Strong typing systems will throw an error if the wrong type is use
by bryanlarsen 14y ago
Lots of people don't realize that strong/weak typing is orthogonal to static/dynamic typing. Strong typing systems will throw an error if the wrong type is used. A weakly typed language will attempt to coerce or cast the variable.
Python is much more strongly typed than both Javascript and C.
Another dimension is the explicitness of your variable languages. This isn't quite orthogonal since static languages have to allow you to specify a type and dynamic languages have to have some way for you to not specify a type but many static languages allow you to infer a type and many dynamic languages allow you to specify types. (You can always use asserts if they don't explicitly allow it).
My point is that inferred types always have the potential to provide surprising results. It is unlikely in static languages, but it's possible. If you've defined "square x = x * x" it will allow you to square anything that has the splat operator defined for, even if that would be nonsensical. For that reason inferred static languages generally don't overload the splat operator, so it's a bad example. But I hope you still get my point.
- draegtun 14y agore: strong/weak/dynamic typing, coercion & overloading And the mix can produce interesting results. For eg. in Python: def double(x): return x * 2 print double(2) # => 4 print double("X") # => XX If you were expecting a number to be returned then this isn't good :( The * operator isn't overloaded in Perl so instead we get: sub double { $_[0] * 2 } say double(2); # => 4 say double('X'); # => 0 Perl is more weakly typed than Python so 'X' get coerced to zero with the multiplication operation. So at least a number is returned but I would prefer a warning or error: use warnings; say double('X'); # produces runtime warning use warnings FATAL => 'all'; say double('X'); # produces runtime exception