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
}
def foo[T] {
var t: T = null.asInstanceOf[T]
t
}
And, if you don't like the ceremony involved in that, you can ease it this way:
// Import this into your scope
case class Init()
implicit def initToT[T](i: Init): T = {
null.asInstanceOf[T]
}
// Then use it
def foo[T] {
var t: T = Init()
t
}
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