I encountered this in both Groovy 1.8.6 and 2.0.0.
So these scenarios all work as expected:
def ay = { one, two=[:] -> [one, two] }
def be = { one, two, three=[:] -> [one,two,three] }
def ayprime = ay.curry('PRIME')
def beprime = be.curry('PRIME')
def beprimer = be.curry('PRIME', 'PRIMER')
assert ay(1,2) == [1,2]
assert ay(1) == [1,[:]]
assert be(1,2,3) == [1,2,3]
assert be(1,2) == [1,2,[:]]
assert ayprime(1) == ['PRIME', 1]
assert ayprime() == ['PRIME', [:]]
assert beprime(1,2) == ['PRIME', 1, 2]
assert beprime(1) == ['PRIME', 1, [:]]
assert beprimer(1) == ['PRIME', 'PRIMER', 1]
assert beprimer() == ['PRIME', 'PRIMER', [:]]
As does this:
class Klass {
static def smethod = { one, two=[:] -> [one, two] }
}
assert Klass.smethod(1,2) == [1, 2]
assert Klass.smethod(1) == [1, [:]]
This also works, as expected:
Klass.metaClass.static.aymethod << ay
assert Klass.aymethod(1) == [1, [:]]
The default parameter to the uncurried closure survives the assignment to Klass
.
However, this fails:
Klass.metaClass.static.ayprimemethod << ayprime
assert Klass.ayprimemethod() == ['PRIME', [:]]
thusly:
assert Klass.ayprimemethod() == ['PRIME', [:]]
| |
[PRIME, null] false
and similarly, this fails:
Klass.metaClass.static.beprimermethod << beprimer
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
thusly:
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
| |
| false
[PRIME, PRIMER, null]
With the curried closures, the default parameter value works directly, but is lost when the closure is assigned as a static member of Klass
.
This seems like a bug. I couldn't find this behavior documented anywhere. Am I missing something?