Question on + and type conversions

Brendan Eich brendan at mozilla.com
Fri Dec 17 14:46:15 PST 2010


On Dec 17, 2010, at 2:01 PM, P T Withington wrote:

>> 11.6.1 The Addition operator(+)
>> ...
>>  7. If Type(lprim) is String or Type(rprim) is String, then
>>   a. Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
>> ...
> 
> Is it intentional that after discovering that one of the operands' primitive value is a string, that toString is called on the other operand's primitive value, rather than its (original) value?

This goes back all the way to JS1 and ES1. It's not new. Is it intended? [Confused slacker voice:] "Kind of?"
 
Seriously, implicit conversions, at least reaching beyond only .toString(), are a '90s era mistake. The .valueOf() implicit calls are related to the boxing of primitives (e.g. 42) with objects (new Number(42)), but the whole system does not hang together very well, and it's no substitute for operator overloading or dynamic (double dispatch for binary) operator methods.


> Because I was hoping:
> 
>  color + '' => 'red'

Implicit conversion magic is not something we are able to (or looking to) change.

Operators, on the other hand, might make it. They are getting some attention now, in the context of value proxies:

http://wiki.ecmascript.org/doku.php?id=strawman:value_proxies

but these would be only for value proxies. In discussing value proxies, at least Cormac, Dave, and I have pondered operators for all objects.

/be


More information about the es-discuss mailing list