Adam Schmideg
Adam Schmideg

Reputation: 10860

Call super when overriding overloaded operator in groovy++

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()

Upvotes: 0

Views: 236

Answers (1)

ataylor
ataylor

Reputation: 66079

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.

Upvotes: 1

Related Questions