Yesterday I bought the book "Programming in Scala" and I'm reading it at my leisure. And only a moment ago I encountered a warning that made me think "argh, why did they do that?".
It turns out there are 2 ways to define a function/method in Scala, one goes like this:
def multiply(x : Int, y : Int) = { x * y }
and the other one like this:
def multiply(x : Int, y : Int) { x * y }
the first one has 2 integer arguments and because of Scala's "type inference" it will also return an integer. This is something you don't have to specify yourself (you could do so and it would look like this def multiply(x : Int, y : Int) : Int but that's not important right now). The 2nd form also has 2 integer arguments, but because we left out the =-sign the result of this method is always "Unit" (like "void" in Java). In other words, the result of the expression will be thrown away quietly.
I think this is one of those things that can really bug you for hours without being able to figure out what is going wrong exactly. It seems like an unnecessary "shortcut", I would have preferred to explicitly define the result as being of type Unit in the case I really wanted to throw away the result.
I think this is one of those things that can really bug you for hours without being able to figure out what is going wrong exactly. It seems like an unnecessary "shortcut", I would have preferred to explicitly define the result as being of type Unit in the case I really wanted to throw away the result.