Recall from the scala book:
--
Signature. The name, parameters list, and result type, if specified, form a method’s signature. After the method’s signature you must put an equals sign and then the body of the method. If the method body consists of just one statement, you need not place it inside curly braces, but you can if you want.
Empty Parameter List. The recommended style guideline for such method invocations is that if the method may have side effects, you should provide the parentheses even if the compiler doesn’t
require them.
def greet() = println("Hello, world!")
Thus in this case, since the greet method prints to the standard output, it has side effects and you should invoke it with parentheses to alert programmers looking at the code.
--
So, when I set up a function but want to stress that it actually is a procedure I go for
def foo() {}
And, as I like to document my code -- at least a bit, I go for
def foo(): type = {}
to set up a function. This entails, I will never have
def foo(): Unit = {}
as this is supposed to be a procedure. For values I go for val foo: type = {}, because of the previously mentioned documentation purpose.
Note: Sometime you want to have def foo: type = {} for values which have to be re-computed every time used.
Scooter tour on Waiheke
15 years ago