Details
-
Type:
New Feature
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: 3.0
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
Right now several operators (e.g. ! && || =~ ==~)¹ cannot be intercepted. Making all operators interceptable would improve language consistency and open up new possibilities for API/DSL designers. For an example of how this feature could be used, see http://www.nabble.com/Operator-Interception-tf3424475.html#a9544816
¹ Also < <= => > <=> cannot be intercepted. Intercepting compareTo() is not enough, because it doesn't tell which operator was used.
Issue Links
| This issue is duplicated by: | ||||
| GROOVY-1888 | Make all operators overridable |
|
|
|
| This issue is related to: | ||||
| GROOVY-2756 | create new user overwritable operator methods for <,==,>,<=,=>,<==> |
|
|
|
I recently ran into this issue when trying to implement a Set operation/notation DSL and it drove me absolutely nuts. Here is the long and short of my issues with using Comparable to override these methods (<=, =>, < and > respectively).
In set notation I want to say:
def a = [1,2,3]
def b = [1,2,3,4,5,6]
def c = [1, 8]
so say that a is a subset of b - a <= b (should return true as all elements of a are in b)
b is a superset of a - b => a (again just like previous example but reversed)
c is not a subset of b - assert !(c <= b) as only one element of c is in b
In the last case I can't logically return -1, 0, or 1 without knowing which method was called as it isn't less than, equal to, or greater than the set it is being compared against. It is simply false.
In the current implementation this is impossible as Groovy doesn't give me direct access to the methods lessThanEqualTo, greaterThanEqualTo, or additionally greaterThan, and lessThan.
By allowing me to explicitly override these methods I can then perform the checks I need to and return true or false depending on the situation instead of letting Groovy try and figure out what I mean for me.