java – Scala点语法(或缺少)

当我遇到一段对我来说没有意义的代码时,我正在经历这本精彩的书 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

但是,当我尝试编译此更改时,会出现以下错误

error: ‘;’ expected but ‘.’ found.
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版中
> A Tour of Scala: Operators

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...