this works:
scala> class foo[T] {
| var t: T = _
| }
defined class foo
but this doesn't:
scala> def foo[T] = {
| var t: T = _
| }
<console>:5: error: local variables must be initialized
var t: T = _
why?
(one can use:
var t: T = null.asInstanceOf[T]
)
This is defined in section 4.2 of the Scala Language Specification (my italics)
A variable definition
var x: T = _
can appear only as a member of a template. It introduces a mutable field with typeT
and a default initial value
This, of course, does not answer the why this should be so!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With