I've been reading up on Scala, since it seems like it may be a better Java than Java itself. As I was reading through the pre-release PDF of Programming in Scala, I came across something goofy.
Scala, like (as I understand it) F#, tries to nestle itself comfortably between the functional and imperative camps. It has a syntax that supports both schools of though. So, as you might expect, some functions in Scala will behave nicely and will return a value without any side effects. Other functions will be executed solely for the side effects, and will return nothing (the Unit type in Scala). To further the Functional mindset, Scala does not require an explicit return statement at the end of a function. Instead, the last value in the function is used as the value of the function. Programming in Scala is quick to point out that, if you want, you can just as easily use explicit return statements (if that floats your boat).
The functional and imperative worlds collide in a shower of fireworks. From Programming in Scala:
One puzzler to watch out for is that whenever you leave off the equals sign before the body of a function, its result type will definitely be Unit. This is true no matter what the body contains, because the Scala compiler can convert any type to Unit. For example, if the last result of a method is a String, but the method’s result type is declared to be Unit, the String will be converted to Unit and its value lost.The book then goes on to provide an example where a function's value is accidentally lost.
Now, I'm all for shortening my programs. The less I have to type, the better. This is, in fact, one of the big advantages Scala has over Java. But wait just a minute! I thought that our compilers were supposed to help us, not trip us up! Here's a situation where 2 different things (a function's return value and the function's return statement) are optional. If they are not specified, they are inferred. In that case, the only difference between retaining and losing your return value is a single character - a '='.
To get all concrete, here are a pair of Scala programs that do different things.
package org.balefrost.demo
object Sample {
def foo {
"bar"
}
def main(args : Array[String]) : Unit = {
val baz = foo
println(baz)
}
}
=> ()
package org.balefrost.demo
object Sample {
def foo = {
"bar"
}
def main(args : Array[String]) : Unit = {
val baz = foo
println(baz)
}
}
=> "bar"
I don't know. To me, that's goofy. Other people might find it completely reasonable. Of course, you can protect yourself with explicit types.
package org.balefrost.demo
object Sample {
def foo {
"bar"
}
def main(args : Array[String]) : Unit = {
val baz:String = foo //compiler error: can't assign Unit to String
println(baz)
}
}
Anyway, kudos to Programming in Scala for pointing out the potential cause of a hair-yankingly-frustrating bug. Now that I understand what's going on, I will probably be better able to handle it when it comes up in a real program.
No comments:
Post a Comment