jsx / JSX

JSX - a faster, safer, easier JavaScript
http://jsx.github.io/
MIT License
1.46k stars 101 forks source link

operator overloading #219

Open nyuichi opened 11 years ago

nyuichi commented 11 years ago

syntax (and the convention) may subject to change, but at least about arithmetic operations, if there were overloaded operators, it would be so useful.

class BigInt {
    function __add__ (n : BigInt) : BigInt {
        return ...;
    }
}

var a = new BigInt(1);
var b = new BigInt(2);
a + b; // => BigInt(3)
gfx commented 11 years ago

+1, bot there're some issues:

nyuichi commented 11 years ago

@gfx

operator==

In that case, we simply prohibit users from overloading the == operator. We might have to disallow && and ||.

operator+(number, BigInt)

The answer: you cannot implement such a method. Fortunately we only have operators which we can alternatively swap the operands or use and combine other operators to do the same thing. So you don't need to overload operators the first argument is of a primitive type.

kazuho commented 11 years ago

operator+(number, BigInt)

The answer: you cannot implement such a method. Fortunately we only have operators which we can alternatively swap the operands or use and combine other operators to do the same thing. So you don't need to overload operators the first argument is of a primitive type.

I doubt if the statement hold valid when a user tries to implement cross-product of vectors using operator overloading. Any ideas?

nyuichi commented 11 years ago

@kazuho

In that case you should not use * operator to do cross-product. Drfining cross or outer method is much better. I guess thr rule that operations breaking the commutative law must not be defined using embedded operators is a very popular convention widely adopted. When operator overloading is included in jsx, we should adopt this convention at that time.