gpt4 book ai didi

scala - Scala 隐式参数的 Groovy 等价物

转载 作者:行者123 更新时间:2023-12-02 05:24:09 24 4
gpt4 key购买 nike

是否有一些 Groovy 替代方案来表达如下内容:

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

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

x = 0
doSomethingWith // Got 0

更新:在此处查看后续问题:Groovy equivalent for Scala implicit parameters - extended

最佳答案

您可以使用带有默认参数的闭包:

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

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

value = 0
doSomethingWith() // Got 0

关于scala - Scala 隐式参数的 Groovy 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13378337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com