为什么地图/过滤器...不是没事的数组工作? [英] Why does map/filter ... not work with an Array of Nothing?

查看:166
本文介绍了为什么地图/过滤器...不是没事的数组工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也并非一无是处所有类型的子类型?

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?

推荐答案

当你看到涉及任何怪异的行为,这是因为该类型推理算法认为它插入没什么本身,因为它是类型推理过程中引入的:如果没有人知道关于类型的变量,然后它是由任何与虚无界。一直以来,我对事物的名单上做,看看这样用户级没有和推断级没有什么不混,我可以介绍一个新仅供内部使用的底层类型用于这一目的,但它是一个pretty雄心勃勃的任务。不过,我现在可能是铁杆足够的尝试吧。

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.

这篇关于为什么地图/过滤器...不是没事的数组工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆