user3663882 user3663882 - 3 months ago 8
Scala Question

Leaving parenthesis in method call

I thought in Scala we can leave paratnehsis when calling methods. I tried to do this, but compiler complains:

object Objd {
def m1(s: String) = {
StringBuilder.newBuilder
.append(s)
.toString()
}

def m2(s : Stirng) = {
StringBuilder.newBuilder
.append("Another string")
.append(";")
.append(m1 s) //compile error. Not applicable to String
.toString()
}
}


Why isn't it possible in such a case?

Answer

You can't do it in general. The rule is that

expr m1 arg1 m2 arg2 ...

means

expr.m1(arg1).m2(arg2)...

In this case you don't have a start to the chain. You could write append(this m1 s).