I observed a difference in Scala's type inference when applied to def
and val
.
Using def
, I can define an abstract nullary method const
returning some value of type Int => Int
. When implementing const
with a function literal, I need not supply a parameter type, as it can be inferred by the compiler:
trait D {
def const: Int => Int
}
object D extends D {
def const = i => i + 1
}
This is fine. (On the downside, a new function instance is being created for every access to D.const
.)
Now consider an analogous construction using val
:
trait V {
val const: Int => Int
}
object V extends V {
val const = i => i + 1
}
This will not compile, failing with
error: missing parameter type
val const = i => i + 1
^
Why?