On Mar 7, 11 21:44, spir wrote:
On 03/07/2011 02:05 PM, Jacob Carlborg wrote:
You could implement operator overloading without any special
cases/support in
the language, like Scala does. In Scala
3 + 4
Is syntax sugar for:
3.+(4)
It's possible because of the following three reasons:
* Everything is an object
* Method names can contain other characters than A-Za-z_
* The infix syntax discussed in this thread
Implementing operator overloading like this also allows you to add new
operators and not just overloading existing ones.
We could give a standard name to each character in an allowed class, so
that
x !%# y
maps to
x.opBangPercentHash(y);
;-)
The current opBinary syntax already allows this ;)
Bar opBinary(string op:"!%#")(Foo y) const {
...
}
Another solution is to specify operators in method defs:
X opBangPercentHash as "!%#" (X y) {...}
Or even use them directly there:
X !%# (X y) {...}
possibly with an annotation to warn the parser:
@operator X !%# (X y) {...}
In any case, /this/ is not a big deal to manage in symbol tables, since
an operator is just a string like (any other) name. The big deal is to
map such features to builtin types, I guess (which are not object types).
The big deal is it makes parsing more difficult (precedence and
associativity need to be determined) with no significant benefit.
Denis