gpt4 book ai didi

Kotlin 类似于 Groovy 中的作用域函数(let、also、apply、run)

转载 作者:行者123 更新时间:2023-12-02 15:53:15 25 4
gpt4 key购买 nike

我认为标题本身就说明了一切——Groovy 有类似 Kotlin 作用域函数的东西吗?

obj.apply {
foo()
bar()
baz()
}

// is the same as
obj.foo()
obj.bar()
obj.baz()

最佳答案

Groovy 有 obj.with { } 方法,可以让你做同样的事情:

obj.with {
foo()
bar()
baz()
}

还有 obj.tap { } 变体(相当于 obj.with(true) { })做同样的事情,但它返回传入的对象.

def newObj = obj.tap {
foo()
bar()
baz()
}

来源:http://docs.groovy-lang.org/docs/next/html/documentation/style-guide.html#_using_with_and_tap_for_repeated_operations_on_the_same_bean

关于Kotlin 类似于 Groovy 中的作用域函数(let、also、apply、run),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71887133/

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