6

Is it possible to define a brand new operator in Groovy? I would like to express a trade where someone buys 200 items for the price of 10 like this:

def trade = 200 @ 10

Is this achievable?

Thanks

EDIT: I want to make it clearer that I am interested in defining an operator not adding a method. Cheers.

Dalibor Novak
  • 575
  • 5
  • 17
  • 3
    No, currently this isn't possible, you have to use existing operators to define your own operations – tim_yates Jun 26 '11 at 20:49
  • It seems that [Spock Boys](http://code.google.com/p/spock/wiki/Interactions#Return_values) have managed to do it with a `>>>` operator which is not mentioned in the [Operator Overloading](http://groovy.codehaus.org/Operator+Overloading) list provided by [mr. Muschko](http://stackoverflow.com/users/528428/benjamin-muschko). Can someone either explain how it can be done or provide a definitve NO? I am affraid I feel that I can't accept current answers. Thanks for bearing with me. – Dalibor Novak Jun 27 '11 at 16:12
  • 1
    The `>>>` operator already exists in both Java and Groovy, it's the `right shift unsigned` operator. I [found it was covered by Groovy last week](http://groovyconsole.appspot.com/script/499001) whilst digging around in the code ;-) – tim_yates Jun 28 '11 at 13:40
  • @Tim Thanks for clearing it out. I accepted Benjamin's answer taking it together with his subsequent comment. – Dalibor Novak Jun 29 '11 at 08:03

4 Answers4

6

We always wanted the ability to define an operator through the user in Groovy, but so far we haven't gotten around the problems that come along with that. So the current state is that Groovy does not support custom operators, only the ones that are already in use.

blackdrag
  • 6,413
  • 2
  • 26
  • 38
2

I am not quite sure how you can make this work for the @ sign but you could certainly add the operation like this which I actually find more expressive:

Number.metaClass.buyFor { Integer price ->
   delegate * price
}

def result = 200.buyFor(10)
println result
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
  • Thanks for the input. Unfortunately this doesn't really answer my question. I am aware that I can add methods via "monkey patching", but I would really like to define an operator. – Dalibor Novak Jun 26 '11 at 20:30
  • 1
    Looks like the `@` sign is not supported yet as [overloadable operator](http://groovy.codehaus.org/Operator+Overloading). If you can settle for any of the available operators you can implement it as stated above. – Benjamin Muschko Jun 26 '11 at 20:52
0
Number.metaClass."@" {Integer x -> delegate * x} 

assert (2.'@' (2)) == 4
han
  • 26
  • 1
  • Thanks for entry. You are close, but no cigar. As I mentioned to Ben I am looking for an answer on the question whether it is possible to define a new OPERATOR not add a method. – Dalibor Novak Jun 26 '11 at 20:45
0

The official documentation has a section on Operator Overloading: https://groovy-lang.org/operators.html#Operator-Overloading

Here is a list from the docs: List of overloadable operators

Ben Creasy
  • 3,825
  • 4
  • 40
  • 50