Here is nothing wrong.
With type parameter not specified scala compiler takes the most concrete type. In case of printType()
it's Nothing
(the bottom type in scala type hierarchy).
Since Nothing
is subtype of Foo
your code is valid:
var foo3 : Foo = printType[Nothing]()
Funny thing is that with var foo3 : Nothing = printType[Nothing]()
or even with printType[Nothing]()
you'll get a NullPointerException
:
scala> var foo3 : Nothing = printType[Nothing]()
Nothing
java.lang.NullPointerException
scala> printType[Nothing]()
Nothing
java.lang.NullPointerException
You can't get an instance of Nothing
, but your code (var foo3 : Foo = printType[Nothing]()
) works due to the type erasure. It looks like a scala bug that can't be fixed.