Isn't Nothing a subtype of all types?
scala> val array = new Array(5)
array: Array[Nothing] = Array(null, null, null, null, null)
scala> array.map(_ => 42)
<console>:9: error: value map is not a member of Array[Nothing]
array.map(_ => 42)
^
scala> array.filter(_ != 42)
<console>:9: error: value filter is not a member of Array[Nothing]
array.filter(_ != 42)
^
It's weird that this doesn't work.
Is this specified, a feature or a bug?
Note that the Scala Array type is invariant. So
Nothing
being a subtype of everything may not be relevant.Also
map
andfilter
are not defined onArray
. Implicit conversions inPredef
are used to provide such methods for arrays.So the compiler is unable to find an implicit conversion from
Array[Nothing]
to something that has themap
orfilter
defined. Using the REPL, I can actually see that such an implicit conversion should be available:So the question becomes why the compiler does not consider the
genericArrayOps
conversion.I suspect Scala shouldn't let you do that kind of
Array[Nothing]
instantiation. There are by definition no instances of nothing around, yet your array looks like it's filled withNothing
s that are null, but null is not a valid value forNothing
. This for instance fails with the errortype mismatch; found : Null(null) required: Nothing
So I'd expect to run into trouble each time you can actually fool the system to believe you are finally getting hold of a much sought for instance of
Nothing
…Here's another weird case. Run this:
When you see weird behavior involving Nothing, it's because the type inference algorithm thinks that it inserted Nothing itself, since it is introduced during type inference: if nothing is known about a type variable then it is bounded by Any and Nothing. It has long been on my list of things to do to see if I can introduce a new internal-only bottom type for that purpose so user-level Nothing and inference-level Nothing are not intermingled, but it's a pretty ambitious task. Still, I might now be hardcore enough to try it.