3 ms·
re: strong/weak/dynamic typing, coercion & overloading And the mix can produce interesting results. For eg. in Python: def double(x): return x * 2 pr
by draegtun 14y ago
re: 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