为什么 map/filter ... 不适用于无数组?
Nothing 不是所有类型的子类型吗?
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)
^
奇怪的是这不起作用。
这是指定的功能还是错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您看到涉及 Nothing 的奇怪行为时,这是因为类型推断算法认为它本身插入了 Nothing,因为它是在类型推断期间引入的:如果对类型变量一无所知,那么它就会受到 Any 和 Nothing 的限制。长期以来,我一直在要做的事情清单上,看看我是否可以为此目的引入一个新的仅限内部的底部类型,以便用户级 Nothing 和推理级 Nothing 不会混合在一起,但这是一项非常雄心勃勃的任务。不过,我现在可能已经足够硬核去尝试了。
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.
我怀疑 Scala 不应该让你进行这种 Array[Nothing] 实例化。根据定义,周围没有任何实例,但您的数组看起来像是充满了 null 的
Nothing
,但 null 不是Nothing
的有效值。例如,此操作会失败,并出现错误类型不匹配; find : Null(null) required: Nothing
因此,每次你实际上可以愚弄系统,让系统相信你终于得到了一个备受追捧的
Nothing
实例时,我预计会遇到麻烦code>...这是另一个奇怪的案例。运行这个:
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:
请注意,Scala 数组类型是不变的。因此,
Nothing
作为所有事物的子类型可能不相关。另外,
map
和filter
未在Array
上定义。Predef
中的隐式转换用于为数组提供此类方法。因此,编译器无法找到从 Array[Nothing] 到定义了 map 或 filter 的隐式转换。使用 REPL,我实际上可以看到这样的隐式转换应该可用:
所以问题变成了为什么编译器不考虑
genericArrayOps
转换。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.