tags:

views:

308

answers:

1

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]

)

+2  A: 

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 type T and a default initial value

This, of course, does not answer the why this should be so!

oxbow_lakes