3 ms·
> For example, in languages like Perl, JavaScript, and CoffeeScript adding a string to a number is permissible (e.g., "5" + 2 yields "52"). The same operation y
by hoytech 9y ago
> For example, in languages like Perl, JavaScript, and CoffeeScript adding a string to a number is permissible (e.g., "5" + 2 yields "52"). The same operation yields 7 in Php. Such an operation is not permitted in languages such as Java and Python as they do not allow implicit conversion.
Regarding Perl, the quoted statement is wrong:
$ perl -E 'say "5" + 2'
7
Furthermore, this is not an implicit conversion. The + operator is an explicit numeric conversion. Here's a more detailed description:
https://codespeaks.blogspot.ca/2007/09/ruby-and-python-overload-operator-for.html https://codespeaks.blogspot.ca/2007/09/ruby-and-python-overl...
- gipp 9y agoYour link seems to argue that the simple fact that it's common convention makes it explicit, which doesn't really seem to hold water.
- hoytech 9y agoConsider this javascript function: function add(a,b) { return a + b; } Although no conversion is requested explicitly in the function definition, a conversion may take place depending on the types of the arguments passed in: > add(1,2) 3 >add("1",2) "12" The article in question defines implicit conversion in this way, and in my experience it's a fairly common term. I was pointing out that per this definition, the article is wrong in saying that perl's + operator may perform an implicit conversion. In perl the + operator always performs a numeric conversion of both its operands, regardless of types. By writing + you are explicitly requesting numeric conversion of both arguments. In general perl doesn't perform implicit conversion (of course there are some exceptions -- it is perl after all). It does this by not overloading operators like + for different operations such as addition and concatenation. This also has the nice property that you can count on a+b == b+a, unlike python for instance. (However, in python PEP-465, non-commutativity was a stated advantage of adopting @ for matrix multiplication instead of overloading *, go figure).
- agrafix 9y agoThe + operator is not an explicit numeric conversion. What numeric type is it converting to? In Python you can also combine two lists using the + operator. Haskell for example requires to be explicit about this: y = (read "5" :: Int) + 5
- hoytech 9y agoPlease see my sibling reply. I was referring to perl, not python or haskell.