Given the following constructs for defining a function in Scala, can you explain what the difference is, and what the implications will be?
def foo = {}
vs.
def foo() = {}
Update
Thanks for the quick responses. These are great. The only question that remains for me is:
If I omit the parenthesis, is there still a way to pass the function around? This is what I get in the repl:
scala> def foo = {}
foo: Unit
scala> def baz() = {}
baz: ()Unit
scala> def test(arg: () => Unit) = { arg }
test: (arg: () => Unit)() => Unit
scala> test(foo)
<console>:10: error: type mismatch;
found : Unit
required: () => Unit
test(foo)
^
scala> test(baz)
res1: () => Unit = <function0>
Update 2012-09-14
Here are some similar questions I noticed:
To answer your second question, just add an
_
:Let me copy my answer I posted on a duplicated question:
A Scala method of 0-arity can be defined with or without parentheses
()
. This is used to signal the user that the method has some kind of side-effect (like printing out to std out or destroying data), as opposed to the one without, which can later be implemented asval
.See Programming in Scala:
If you include the parentheses in the definition you can optionally omit them when you call the method. If you omit them in the definition you can't use them when you call the method.
Additionally, you can do something similar with your higher order functions:
Here
baz
will only takefoo()
and notbar
. What use this is, I don't know. But it does show that the types are distinct.I would recommend always start definition with a function like:
and only in cases, when you are forced, to change it to:
Reason: Let's consider these 2 functions from a point of possible usage. How they can be infoked AND where they can be passed.
I would not call this a function at all:
It can be invoked as:
but not as a function:
We can use this bar when we define a higher-order function with a call-by-name parameter:
We should remember, that
=> Unit
- is not even a function. You absolutely cannot work with a thunk as if it's a function insofar as you cannot choose to treat it as Function value to be stored or passed around. You can only trigger evaluations of the actual argument expression (any number of them). Scala: passing function as block of code between curly bracesA function, defined with
()
has a bigger scope for usage. It can be used exactly, in the same context, asbar
:It can be passed into a function with a call-by-name parameter:
Additionally, if we define a higher-order function, that accepts not a call-by-name pamameter, but a real function:
We can also pass
foo
to thebaz
:As we can see standard functions like
foo
have a bigger scope for usage. But using a functions defined without()
plus defining higher-order functions, that accept call-by-name parameter, let us use more clear syntax.If you do not try to archive a better, more readable code, or if you need ability to pass your piece of code both to function defined with a call-by-name parameter and to a function defined with a real function, then define your function as standard one:
If you prefer to write more clear and readable code, AND your function has no side-effects, define a function as:
PLUS try to define your higher-order function to accept a call-by-name parameter, but not a function. Only when you are forced, only in this case use the previous option.