0

I am learning scala, and meet a little problem. I want to 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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.