26 Nov 2004 13:13
operator overloading syntax and methods...
We should be able to write code fairly cleanly to overload operators.
How about some syntax like this..
class Cheese {
String operator + (String param) {
return param + " cheese!"
}
operator [] (Object key) {
// overload how we lookup items using subscript operator
return key * 2
}
}
basically as a bit of syntax to allow the operator overloading methods
to be defined. Using a keyword 'operator' followed by the operator in
question as an alternative to using a mangled method name. It'd then
map to a regular method which would appear in the bytecode.
Then we just need to figure out what the method names should be. We
went initially with nice simple methods in Groovy (add, subtract etc).
I'm wondering if its better if we mangle the names under the hood to
avoid clashing with legacy Java code - but use a neater syntax in
Groovy code. If we use a mangled Java method name at the bytecode level
then Java developers could add operator overloading methods to their
Java code.
For a name mangling format, how about
// this is Java code
public class Cheese {
public String _operatorAdd(String param) {
return param + "cheese!";
}
}
Thoughts?
James
-------
http://radio.weblogs.com/0112098/
RSS Feed