gpt4 book ai didi

scala - 不带括号的对象方法调用

转载 作者:行者123 更新时间:2023-12-01 12:43:04 26 4
gpt4 key购买 nike

导入后是否可以调用不带括号的对象方法?

这是我的测试代码,基于 this article马丁·奥德斯基:

package gardening.fruits

object PrintPlanted {
def main(args: Array[String]) {
// Call method on object
import gardening.fruits
fruits showFruit(apple)
fruits showFruit apple

// Import object
import gardening.fruits._
showFruit(apple)

// showFruit apple

// Error: missing arguments for method showFruit in package fruits;
// follow this method with `_' if you want to treat it
// as a partially applied function
}
}

导入包对象后 fruits ,我可以调用方法 showFruit(apple) ,但是有没有什么方法可以像最后一段代码那样不带括号地调用该方法?

我在一些 DSL 中使用了这样的方法,使用括号有点烦人。欢迎您提出所有建议。

最佳答案

不,那是不可能的。 e1 op e2是所谓的中缀操作,相当于e1.op(e2) .当接收器( e1 )被排除在外时,这不起作用。出于同样的原因,您不能写出类似 println "hello" 的内容。 , 任何一个。

关于scala - 不带括号的对象方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542159/

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