2016-08-26 23 views
0

我認爲在Scala中調用方法時可能會失去偏見。我試圖做到這一點,但編譯器抱怨:在方法調用中留下圓括號

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() 
    } 
} 

爲什麼在這種情況下不可能?

回答

1

你一般不能這樣做。規則是

expr m1 arg1 m2 arg2 ... 

意味着

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

在這種情況下,你沒有一個開局鏈。你可以寫append(this m1 s)

相關問題