Implicit conversions are quite possible to do. The situation where you get in trouble is when you don't know which way something should work.
An example of this can be seen in Javascript where the +
operator works in different ways at different times.
>>> 4 + 3
7
>>> "4" + 3
43
>>> 4 + "3"
43
If one of the arguments is a string, then the +
operator is a string concatenation, otherwise it is addition.
If you are given an argument and don't know if it is a string or an integer, and want to do addition with it, it can be a bit of a mess.
Another way to deal with this is from the Basic heritage (that perl follows from - see Programming is Hard, Let's Go Scripting...)
In Basic, the len
function only makes sense being invoked on a String (docs for visual basic: "Any valid String expression or variable name. If Expression is of type Object, the Len function returns the size as it will be written to the file by the FilePut function.").
Perl follows this concept of context. The confusion that exists in JavaScript with implicit conversion of types for the +
operator being sometimes addition and sometimes concatenation doesn't happen in perl because +
is always addition and .
is always concatenation.
If something is used in a scalar context, its a scalar (e.g. using an list as a scalar, the list behaves as if it was a number corresponding to its length). If you use a string operator (eq
for equality test, cmp
for string comparison) the scalar is used as if it was a string. Likewise, if something was used in a math context (==
for equality test and <=>
for numerical comparison), the scalar is used as if it was a number.
The fundamental rule for all programming is "do the thing that surprises the person the least". This doesn't mean there aren't surprises in there, but the effort is to surprise the person the least.
Going to a close cousin of perl - php, there are situations where an operator can act on something in either string or numeric contexts and the behavior can be surprising to people. The ++
operator is one such example. On numbers, it behaves exactly as expected. When acting on a string, such as "aa"
, it increments the string ($foo = "aa"; $foo++; echo $foo;
prints ab
). It will also roll over so that az
when incremented becomes ba
. This isn't particularly surprising yet.
$foo = "3d8";
echo "$foo\n";
$foo++;
echo "$foo\n";
$foo++;
echo "$foo\n";
$foo++;
echo "$foo\n";
(ideone)
This prints out:
3d8
3d9
3e0
4
Welcome to the dangers of implicit conversions and operators acting different on the same string. (Perl handles that code block a bit differently - it decides that "3d8"
when the ++
operator is applied is a numeric value from the start and goes to 4
right away (ideone) - this behavior is well described in perlop: Auto-increment and Auto-decrement)
Now, why one language does something one way and another does it another way gets to the design thoughts of the designers. Perl's philosophy is There's more than one way to do it - and I can think of a number of ways of doing some of these operations. On the other hand, Python has a philosophy described in PEP 20 -- The Zen of Python which states (among other things): "There should be one-- and preferably only one --obvious way to do it."
These design differences have lead to different languages. There is one way to get the length of a number in Python. Implicit conversion goes against this philosophy.
Related reading: Why doesn't Ruby have implicit conversion of Fixnum into String?
perl -e 'print length(100);'
prints 3.str
be the implicit way to convert an int into an iterable? how aboutrange
? orbin
(hex
,oct
) orchr
(orunichr
)? All those return iterables, even ifstr
seems the most obvious for you in this situation.