25

In Scala there are two ways to define a method which takes no argument

    1 def a=println("hello")

    2 def a()=println("hello")

These two methods are exactly same but (2) can be called with and without parentheses.

Is there any special reason for which this feature is allowed in Scala.It confuses me which to use and when?

2
  • 2
    Not to confuse you further, but try def a()()=println("hello")....
    – Rex Kerr
    Commented Aug 4, 2011 at 12:39
  • 4
    These are methods, not functions. Commented Aug 4, 2011 at 15:04

3 Answers 3

25

The general rule is that you should add an empty parameter list at both declaration site and call site whenever the method (not function) has side effects.

Otherwise, Scala has the uniform access principle, so that clients don't need to know whether they're accessing a field or calling a side-effect-free method.

4
  • Side effect functions are those which does not return anything and do some I/O or changes state. Am I right? What about function which does I/o, change state and still return something?
    – codecool
    Commented Aug 4, 2011 at 11:02
  • 4
    Whether it returns something doesn't matter. Commented Aug 4, 2011 at 11:49
  • The purpose of the convention is that the empty parameter list warns the user that some side effect is occurring as a result of calling the method, regardless of return value. Commented Aug 4, 2011 at 16:27
  • 2
    @codecool, side effects functions are those subrutines that modifies internal state. THe word side effects can be misleading without proper context.. sometimes this effect is what you want and expect to happen.
    – Victor
    Commented Jun 9, 2016 at 14:34
10

The syntax without parenthesis is allowed so one can write this:

abstract class X {
  def f: Int
}

class Y extends X {
  val f = 0
}

A code calling f on an X does not need to know if it is a val or a def.

The reason why one can omit parenthesis when calling methods with an empty list is to allow calling Java methods that would ideally not have parenthesis (but, because they are Java, they all have parenthesis).

As other said, there's a convention of using an empty parameter list when the method has side effects, and leaving them off otherwise.

2
  • can you expand on why you described the defs in the question as methods and not functions? Can't defs be turned into functions if passed into a higher-order function? or assigned to a function type Unit => Unit?
    – Toby
    Commented Sep 10, 2017 at 11:42
  • @Toby Functions and methods are different things, and, here, I'm talking about methods. Commented Sep 15, 2017 at 4:56
8

It's a matter of style whether you choose to use parentheses to indicate a side-effecting method call.

By the way, if you declare a purely side-effecting method using =, you should probably explicitly declare a Unit return type, like this:

def a: Unit = println("hello")

Note that any type can be coerced to Unit.

If you do not want to explicitly declare a return type, you should probably omit the =. Then the compiler will infer a return type of Unit, even if the last expression returns something different:

def a() { println("hello") }

Both of the above styles make refactoring safer, because modifying the method body will never cause the compiler to infer a different return type. IMO this explicitness of declaration is more important than call-site code style.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.