Steven Levine
Steven Levine
~1 min read

Tags

I installed the latest Groovy Beta, (1.6) and no doubt it feels quite faster. Everything looks good from a performance perspective.

I have run in to some strange behavior dealing with named parameters.

Let me illustrate the loophole I am describing with some sample code.

Lets take this simple Stock Groovy Bean:

class Stock {
    String symbol, name

    Stock(String symbol) {
        this.symbol = symbol
    }
}

def apple = new Stock(symbol :'aapl', name : 'Apple Corp')

assert 'aapl' == apple.symbol
assert 'Apple Corp' == apple.name

If you were to run this code under Groovy 1.5, it runs fine without issue. I know this code has an “error” in it, namely, the fact that there is use of named parameters which do not match that of the explicit constructor.

If you run this same code, unchanged under Groovy 1.6B you receiving the following Exception:

Caught: groovy.lang.GroovyRuntimeException: failed to invoke constructor:
public Stock(java.lang.String) with arguments: {}
    reason: java.lang.IllegalArgumentException: wrong number of arguments
    at NamedParmTest.run(NamedParmTest.groovy:11)
    at NamedParmTest.main(NamedParmTest.groovy)

I am not suggesting there is anything wrong here, I am simply pointing out that the latest version of Groovy became more “Strict”.