Java – Scala point syntax (or missing)
When I came across a piece of code that meant nothing to me, I was experiencing this wonderful book 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) }
Notes 2 and 3:
val this1 = this widen that.width
It seems that I can use the following instead:
val this1 = this.widen that.width
However, when I try to compile this change, the following error occurs:
Why is this grammar unacceptable?
Solution
Line 2 uses the extension method as an operator instead of using it as a method in Java:
val this1 = this.widen(that.width)
The error occurred because you have omitted parentheses. You can only do this when using methods in operator symbols You cannot do this:
"a".+ "b" // error: ';' expected but string literal found.
Instead, you should write
"a".+ ("b")
In fact, you can do it with integers, but it's beyond the scope of this problem
Read more:
>Section 3 of Chapter 5 of your book is about operators. At least in the first edition and the fifth edition > a tour of scala: operators