Possible Duplicate:
Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?
Consider we have a class Foo with a method bar (which does takes no arguments and return the string "bar"). There are two ways to implement bar
The first one is
class Foo {
def bar() = "bar"
}
The second one is
class Foo {
def bar = "bar"
}
While both do basically the same, they need to be called differently, the first one like this
someFoo.bar()
and the second one
someFoo.bar
Why should I use one over the other and what’s the fundamental difference?
Defining a method without arguments without parentheses implies that the method is pure (it has no side effects and doesn’t depend on the state of program). Such methods cannot be called with parentheses:
Calling a method without arguments with parentheses implies that the method has some side effects and a return type of
Unit. A method defined with empty parentheses can be called with or without them.