Groovy等价于Scala隐式参数 [英] Groovy equivalent for Scala implicit parameters

查看:134
本文介绍了Groovy等价于Scala隐式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些Groovy替代方法可以表达如下内容:

  def doSomethingWith(implicit i:Int)= println( got+ i)
implicit var x = 5

doSomethingWith(6)//得到6
doSomethingWith //得到5

x = 0
doSomethingWith //得到0

更新:在这里查看后续问题:格罗维等价的Scala隐式参数 - 扩展

  doSomethingWith = <解析方案>  value = 5 

doSomethingWith(6)//得到6
doSomethingWith()//得到5

值= 0
doSomethingWith()//得到0


Is there some Groovy alternative to express something like the following:

def doSomethingWith(implicit i:Int) = println ("Got "+i)
implicit var x = 5

doSomethingWith(6)  // Got 6
doSomethingWith     // Got 5

x = 0
doSomethingWith     // Got 0

Update: see a followup question here: Groovy equivalent for Scala implicit parameters - extended

解决方案

You can use closures with a default parameter:

doSomethingWith = { i = value -> println "Got $i" }
value = 5

doSomethingWith(6)  // Got 6
doSomethingWith()   // Got 5

value = 0
doSomethingWith()   // Got 0

这篇关于Groovy等价于Scala隐式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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