Am I the only one who thinks that this is just wrong...?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
As long as the + operator has been overloaded, this overloading of * seems consistent.
However in all these languages, 3 * "foo" results in an error. So * is not symmetric when applied between numbers and strings. Then again, + isn't symmetric either:
"a" + "b"
is not the same as
"b" + "a"
"foo" + 1 + 2
evaluates differently then
1 + 2 + "foo"
- even in Java. [ September 24, 2008: Message edited by: Mike Simmons ]
posted 11 years ago
In Groovy, this is done with operator overloading. Meaning that I can write my own class that can overload the '*' operator. For example...
I have provided two multiply() method implementations that overloads the '*' operator. When Groovy sees a '*' in the code, it looks for a method on the preceeding object called 'multiply'. In many cases, this is Number, so the Number.multiply(Number operand) method is called. If it is not a Number, but a Cat, Groovy will still find the right multiply method if you have provided it.