我在 Groovy 1.8.6 和 2.0.0 中都遇到过这种情况。
所以这些场景都按预期工作:
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', [:]]
就像这样:
class Klass {
static def smethod = { one, two=[:] -> [one, two] }
}
assert Klass.smethod(1,2) == [1, 2]
assert Klass.smethod(1) == [1, [:]]
正如预期的那样,这也有效:
Klass.metaClass.static.aymethod << ay
assert Klass.aymethod(1) == [1, [:]]
uncurried 闭包的默认参数在分配给Klass
.
但是,这失败了:
Klass.metaClass.static.ayprimemethod << ayprime
assert Klass.ayprimemethod() == ['PRIME', [:]]
因此:
assert Klass.ayprimemethod() == ['PRIME', [:]]
| |
[PRIME, null] false
同样,这失败了:
Klass.metaClass.static.beprimermethod << beprimer
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
因此:
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
| |
| false
[PRIME, PRIMER, null]
使用柯里化闭包,默认参数值直接起作用,但是当闭包被分配为Klass
.
这似乎是一个错误。我在任何地方都找不到这种行为。我错过了什么吗?