链接没有句点的方法调用时,Scala“不接受参数”
发布时间:2020-12-16 18:54:25 所属栏目:安全 来源:网络整理
导读:我有一节课: class Greeter { def hi = { print ("hi"); this } def hello = { print ("hello"); this } def and = this} 我想打电话给新的Greeter().hi.and.hello作为新的Greeter()嗨和你好 但结果是: 错误:Greeter不接受参数 ??????????????嗨你好 ????
我有一节课:
class Greeter { def hi = { print ("hi"); this } def hello = { print ("hello"); this } def and = this } 我想打电话给新的Greeter().hi.and.hello作为新的Greeter()嗨和你好 但结果是: 错误:Greeter不接受参数 我相信这意味着Scala正在接受这个并试图通过和.但并不是一个对象.我可以传递什么来应用链接调用和方法? 解决方法
你不能像这样链接无参数方法调用.没有圆点和圆括号的一般语法是(非正式地):
对象方法参数方法参数方法参数… 当你编写新的Greeter()hi和hello时,它被解释为方法hi的参数. 使用postfix语法,您可以: ((new Greeter hi) and) hello 但是除了你绝对需要这种语法的专业DSL之外,这并不是真正推荐的. 这是你可以玩的东西,以获得你想要的东西: object and class Greeter { def hi(a: and.type) = { print("hi"); this } def hello = { print("hello"); this } } new Greeter hi and hello (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |