This compiles:
def fibonacci():() => Int = {
var first = 1
var second = 2
return () => {
val oldFirst = first
first = second
second = second + oldFirst
second
}
}
This doesn’t:
def fibonacci():() => Int = {
var first = 1
var second = 2
return ():Int => {
val oldFirst = first
first = second
second = second + oldFirst
second
}
}
I am explicitly trying to tell it that I’m returning an Int, but I get this error: Illegal start of declaration, and it’s pointing to the first = second line. How are they different? I am using Scala 2.8.1.
return (): Int => {...}is not a proper expression in Scala. If you wanted to specify the return type explicitly, you’ll need to put the declaration after the value (and the value would be the anonymous function):Note, however, that there is no need in doing that. If you omit
return, you do not need to make any explicit type declaration at all: