3 ms·
> We can see something similar in languages like Ruby or Perl, where it's actually kind of nice that you don't have to worry about whether a variable holds a st
by kbenson 11y ago
> We can see something similar in languages like Ruby or Perl, where it's actually kind of nice that you don't have to worry about whether a variable holds a string or integer or whatever. (For some reason, Javascript's implementation of the same idea seems to be a bit more annoying, but that could just be me).
Well, for Perl that's likely because the conversion of the variable to the desired type is controlled by the operator, and as such they make distinctions between operators that work on numeric types (the usual assortment, +,-,/,*,==,!=,<,>,<=,>= etc) and those that work on strings (".",x,eq,ne,lt,gt,le,ge, etc). This makes the intended operation obvious, so explicit casting between types is often not required.
For example, in JS, what is the following doing? String concatenation or addition? What if the types are mixed?
var c = a + b;
In Perl, the equivalent is one of the following, depending on your purpose.
my $c = $a + $b; # Addition
my $c = $a . $b; # Concatenation
Note there is no ambiguity. If warnings are enabled, if a string can't be cleanly converted to a numeric type, it will report as much on STDERR. This all stems from a core tenet of Perl, similar things should look similar, different things should look different. String concatenation and addition aren't really similar, as much as they may seem like it at first blush (for example, concatenation is not commutative).
As for Ruby... I don't know. It uses + for string concatenation (but also concat, I believe). I suspect having actual methods to disambiguate intention helps for Ruby.