开发者

Scala method syntax problem

I am learning scala, and meet a little problem. I want to开发者_Go百科 use the String method stripMargin as following. But I can not add parenthesis on this method. I remember that no-argument method's parenthesis is optional, so here why I can not add parenthesis ?

val str=""" hello world
    |" ANd soe"
    |" to world"""

println(str.stripMargin())  // won't compile
println(str.stripMargin)  // compiles successfully


If the method without parameters in define with (), then writing () is optional. If the method is defined without (), then adding () is not allowed (or, rather, is interpreted as if you were calling apply() on the returned result).

Good practice recommends that a method without side effects be defined without (), and to keep () both at definition site and call site when it has side effects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜