java – Scala点语法(或缺少)
发布时间:2020-12-14 05:26:32 所属栏目:Java 来源:网络整理
导读:当我遇到一段对我来说没有意义的代码时,我正在经历这本精彩的书 Programming in Scala: def above(that: Element): Element = { val this1 = this widen that.width val that1 = that widen this.width elem(this1.contents ++ that1.contents)} 注2和3: v
当我遇到一段对我来说没有意义的代码时,我正在经历这本精彩的书
Programming in Scala:
def above(that: Element): Element = { val this1 = this widen that.width val that1 = that widen this.width elem(this1.contents ++ that1.contents) } 注2和3: val this1 = this widen that.width 看来我应该可以用以下代替: val this1 = this.widen that.width 但是,当我尝试编译此更改时,会出现以下错误:
为什么这种语法不可接受? 解决方法
第2行使用扩展方法作为运算符,而不是以Java的方式将其用作方法:
val this1 = this.widen(that.width) 发生错误是因为您已经省略了括号,您只能在运算符符号中使用方法时执行此操作.你不能这样做: "a".+ "b" // error: ';' expected but string literal found. 相反,你应该写 "a".+ ("b") 实际上你可以用整数来做,但这超出了这个问题的范围. 阅读更多: >您的书的第5章第3节是关于操作符的符号,至少在第一版第5版中 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |