Ivan
Ivan

Reputation: 64227

Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?

As far as I understand, in Scala we can define a function with no parameters either by using empty parentheses after its name, or no parentheses at all, and these two definitions are not synonyms. What is the purpose of distinguishing these 2 syntaxes and when should I better use one instead of another?

Upvotes: 30

Views: 12178

Answers (4)

Nirvana
Nirvana

Reputation: 93

I have another light to bring to the usefulness of the convention encouraging an empty parentheses block in the declaration of functions (and thus later in calls to them) with side effects.

It is with the debugger.

If one add a watch in a debugger, such as, say, process referring for the example to a boolean in the focused debug context, either as a variable view, or as a pure side-effect free function evaluation, it creates a nasty risk for your later troubleshooting. Indeed, if the debugger keeps that watch as a try-to-evaluate thing whenever you change the context (change thread, move in the call stack, reach another breakpoint...), which I found to be at least the case with IntelliJ IDEA, or Visual Studio for other languages, then the side-effects of any other process function possibly found in any browsed scope would be triggered...

Just imagine the kind of puzzling troubleshooting this could lead to if you do not have that warning just in mind, because of some innocent regular naming. If the convention were enforced, with my example, the process boolean evaluation would never fall back to a process() function call in the debugger watches; it might just be allowed in your debugger to explicitly access the () function putting process() in the watches, but then it would be clear you are not directly accessing any attribute or local variables, and fallbacks to other process() functions in other browsed scopes, if maybe unlucky, would at the very least be very less surprising.

Upvotes: 0

Alain O'Dea
Alain O'Dea

Reputation: 21716

Scala Style Guide says to omit parentheses only when the method being called has no side-effects: http://docs.scala-lang.org/style/method-invocation.html

Upvotes: 15

Adam Rabung
Adam Rabung

Reputation: 5224

Other answers are great, but I also think it's worth mentioning that no-param methods allow for nice access to a classes fields, like so:

person.name

Because of parameterless methods, you could easily write a method to intercept reads (or writes) to the 'name' field without breaking calling code, like so

def name = { log("Accessing name!"); _name }

This is called the Uniform Access Principal

Upvotes: 1

Dave Griffith
Dave Griffith

Reputation: 20515

It's mostly a question of convention. Methods with empty parameter lists are, by convention, evaluated for their side-effects. Methods without parameters are assumed to be side-effect free. That's the convention.

Upvotes: 46

Related Questions