开发者

groovy list spreading

If you run the following Groovy code, the assertion passes

def foo(a, b) {
  a + b
}

assert 'aaabbb' == foo(['aaa', 'bbb'])

This suggests that if a method is called with a List parameter that contains X elements, then the List will be spread and a method with X arguments will b开发者_如何学编程e invoked.

Of course, this will only happen if there isn't a method defined with a single parameter of type List (or ancestor thereof).

I only discovered this quite recently when reading another SO Groovy answer. I've never seen it mentioned in the Groovy docs, release notes, or books. Is it a hidden feature, a bug, or just something I've missed?


Going to be removed in Groovy 2 apparently: http://groovy.329449.n5.nabble.com/removing-features-in-Groovy-2-td4422494.html

JT's first on the to-remove list and it seems everyone (with clout) on Groovy User agrees.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜