Adding closed curry as a static property using expando metadata loses the default setting

I came across this in both Groovy 1.8.6 and 2.0.0.

Thus, these scripts 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', [:]] 

How does it do:

 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 setting for hidden closure preserves the Klass assignment.

However this one does not work:

 Klass.metaClass.static.ayprimemethod << ayprime assert Klass.ayprimemethod() == ['PRIME', [:]] 

Thus:

 assert Klass.ayprimemethod() == ['PRIME', [:]] | | [PRIME, null] false 

and similarly this fails:

 Klass.metaClass.static.beprimermethod << beprimer assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]] 

Thus:

 assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]] | | | false [PRIME, PRIMER, null] 

With closed gates, the default value of the parameter works directly, but is lost when the close is assigned as a static member of Klass .

This seems like a mistake. I could not find this behavior anywhere. Did I miss something?

+6
source share
1 answer

If the problem still bothers you, I think this may be a workaround until it is fixed in the groovy trunk. Python path to curry:

 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') } 
0
source

Source: https://habr.com/ru/post/920992/


All Articles