Groovy,什么 - >意思 [英] Groovy , what does -> mean

查看:128
本文介绍了Groovy,什么 - >意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在groovy代码的例子中,我在任何地方都可以找到 - > 操作符,但groovy教程和我似乎都没有提供任何关于这意味着什么的解释。

解决方案

它用于分隔您从实际代码中为闭包声明绑定的位置,例如:


$ b

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

之前的部分 - > 声明闭包有两个名为 x y 的参数,而第二部分是闭包的代码。



您可以用一个参数在闭包中省略它,在这种情况下,假设 it 变量:

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

但您也可以做

$ p $ [1, 2,3,4]。每个{lol - > println lol * 2}


In examples of groovy code I find the -> operator everywhere, but the groovy tutorials nor the book I have seem to provide 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,什么 - >意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆