Initializing Generic Variables in Scala

前端 未结 2 1077

How do I declare a generic variable in Scala without initializing it (or initializing to any value)?

def foo[T] {
   var t: T = ???? // tried _, null
   t
}
         


        
2条回答
  •  死守一世寂寞
    2021-02-20 07:45

    You can't not initialize local variables, but you can do so for fields:

    scala> class foo[T] {
         | var t: T = _
         | }
    defined class foo
    

提交回复
热议问题