开发者

Groovy , what does -> mean

开发者 https://www.devze.com 2023-01-02 10:13 出处:网络
In examples of groovy code I find the -> operator everywhere, but the groovy tutorials nor the book I have seem to provid开发者_StackOverflow中文版e any explaination as to what this means.It is use

In examples of groovy code I find the -> operator everywhere, but the groovy tutorials nor the book I have seem to provid开发者_StackOverflow中文版e any explaination as to what this means.


It is used to separate where you declare bindings for your closure from the actual code, eg:

def myClosure = { x, y -> x + y }

the part before -> declares that the closure has two arguments named x and y while the second part is the code of the closure.

You can omit it in closures with just one parameter, in that case the it variable is assumed:

[1, 2, 3, 4].each{ println it*2 }

but you could also do

[1, 2, 3, 4].each{ lol -> println lol*2 }


Groovy , what does -> mean

0

精彩评论

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