6

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?

David Pisoni
  • 2,763
  • 2
  • 20
  • 26
  • I'm seeing the same behavior. I also tested it as a class-instance method (so it could be tested via `Klass.metaClass.ayprimemethod << ayprime; assert new Klass().ayprimemethod() == ['PRIME', [:]]`), and found the same issue. Looks like you should [file a bug in JIRA](http://jira.codehaus.org/secure/BrowseProject.jspa?id=10242). – OverZealous Jul 21 '12 at 04:52
  • Done. http://jira.codehaus.org/browse/GROOVY-5621 – David Pisoni Jul 23 '12 at 15:37
  • Thanks for the explanation of currying :) – Rick Mangi Jul 23 '12 at 19:49

1 Answers1

0

If the problem is still bothering you, i think this might be a workaround, until it gets fixed in groovy trunk. The python way to curry stuff:

def ayprime = { x -> x ? ay('PRIME', x) : ay('PRIME') }
def beprime = be.curry('PRIME')
def beprimer = { x -> x ? be('PRIME', 'PRIMER', x) : be('PRIME', 'PRIMER') }
Will
  • 13,630
  • 1
  • 37
  • 42