Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在Scala中像Int这样的基本类型会被擦除为Object?_Scala_Type Erasure_Autoboxing - Fatal编程技术网

为什么在Scala中像Int这样的基本类型会被擦除为Object?

为什么在Scala中像Int这样的基本类型会被擦除为Object?,scala,type-erasure,autoboxing,Scala,Type Erasure,Autoboxing,在斯卡拉 { x: Option[Int] => x } .getClass .getMethod("apply", classOf[Option[_]]) .getGenericParameterTypes 返回数组(scala.Option)。我最初希望看到的是Array(scala.Option),但我看到的是一个值类(extendsAnyVal)“其实例不由底层主机系统表示为对象” 不过,我仍然不理解对对象的擦除。它难道不是更有用的java.lang.Integ

在斯卡拉

{ x: Option[Int] => x }
   .getClass
   .getMethod("apply", classOf[Option[_]])
   .getGenericParameterTypes
返回
数组(scala.Option)
。我最初希望看到的是
Array(scala.Option)
,但我看到的是一个值类(extends
AnyVal
)“其实例不由底层主机系统表示为对象”

不过,我仍然不理解对
对象的擦除。它难道不是更有用的
java.lang.Integer

它难道不是更有用的
java.lang.Integer

是的,甚至曾经是这样。不幸的是,这会导致类型签名被破坏。也就是说,如果将
Int
擦除为
java.lang.Integer
,则不可能在所有情况下生成正确的字节码


关于这一点,没有单一的票证或提交,但改变这一特定行为的是,在。

中,这不是Scala所做的,而是Java。为了像在非对象中一样,从“GenericParameterTypes”(Java类对象的方法)得到有用的结果,必须创建一个子类。Scala没有为
Option[Int]
创建子类,比如
OptionInt
。(参见@specialized,或许?)@pst;我认为你走错了方向。将上面的
Int
更改为
Symbol
,您将得到
Array(scala.Option)
。嗯,我的假设是这样的:-/I责备编译器的魔力。谢谢。我查看了git log--grep=Integer
,但没有看到任何明显的内容。@ScottMorrison您应该搜索“签名”。不管怎样,我给你找到了。见修改后的答案。