Little problem with the "+" operator

Ben Hutchings ben.hutchings at roundpoint.com
Tue Mar 6 16:42:31 EST 2001


joconnor at cybermesa.com (Jay O'Connor) writes:
<snip>
> Actually, JavaScript will allow you to add numbers to strings, with
> weird results
> 
> '100' + 200 = '100200'
> 100 + '200' = 300
> 
> I *think* that's how it works, I just know it's not very safe	

So my mathematics teachers were lying when they said addition was
commutative!

Seriously, that's just awful behaviour.  I now have this vision of
what someone at Netscape must have written when hacking JavaScript
(neé LiveScript) together:

    class LiveScriptObject {
        virtual LiveScriptObject * add(LiveScriptObject const * other) = 0;
    };
    class LiveScriptInteger {
        virtual LiveScriptObject * add(LiveScriptObject const * other) {
            return new LiveScriptInteger(integer_ + other->toInteger());
        }
    };
    class LiveScriptString {
        virtual LiveScriptObject * add(LiveScriptObject const * other) {
            return new LiveScriptString(string_ + other->toString());
        }
    };

Yes, addition is *obviously* just a function that's dynamically
dispatched on its left argument.  Any web page author should know
*that*.

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list