Call super when overriding overloaded operator in groovy++

240 Views Asked by At

This is the code I have

@Typed class FooMap extends LinkedHashMap {
  def doSomeFoo() {
    // ...
  }

  FooMap plus(Collection coll) {
    super.plus(coll)
  }
}

While it works in plain Groovy, compiling it with Groovy++ gives an error: Cannot reference default groovy method 'plus' using 'super'. Call the static method instead. I don't if it's a bug in Groovy++, or it's meant to work this way. Anyway, I want to call super in a typed way. How can I workaround this situation?

The reason why I want such a method is that I want this code to compile.

FooMap map = new FooMap() + [bar: 42]
map.doSomeFoo()
1

There are 1 best solutions below

0
On BEST ANSWER

I'm not sure exactly why groovy++ doesn't allow the super method to be called, but the static method it refers to is in org.codehaus.groovy.runtime.DefaultGroovyMethods:

import org.codehaus.groovy.runtime.DefaultGroovyMethods

assert DefaultGroovyMethods.plus([one: 1], [two: 2]) == [one: 1, two: 2]

You can get the behaviour you're looking for by calling that.