为什么将成员变量声明为元组时会出现擦除警告?
看一下这个 Scala 类:
class Example {
val (x, y): (Int, Int) = (1, 2)
}
编译此结果会产生警告:
Example.scala:2: warning: non variable type-argument Int in type pattern
(Int, Int) is unchecked since it is eliminated by erasure
val (x, y): (Int, Int) = (1, 2)
^
删除显式类型注释可以消除此警告:
class Example {
val (x, y) = (1, 2)
}
为什么我会收到警告以及为什么删除显式类型注释可以消除它?据我所知,没有任何真正的变化,x
和 y
仍然是 Int
类型,没有类型注释。
Have a look at this Scala class:
class Example {
val (x, y): (Int, Int) = (1, 2)
}
Compiling this results in a warning:
Example.scala:2: warning: non variable type-argument Int in type pattern
(Int, Int) is unchecked since it is eliminated by erasure
val (x, y): (Int, Int) = (1, 2)
^
Removing the explicit type annotation gets rid of this warning:
class Example {
val (x, y) = (1, 2)
}
Why do I get the warning and why does removing the explicit type annotation get rid of it? As far as I can see nothing really changes, x
and y
are still of type Int
without the type annotation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将示例重写为:
此模式匹配实际上由 2 个匹配组成 - 它现在表示:采用
Tuple2[Int, Int]
类型的右侧对象并调用方法unapply[
。然后,Tuple2
伴随对象上的 Int, Int]unapply[Int, Int]
将验证该对象是否确实具有Tuple2
类型,并且其结果值将用于将值绑定到变量x< /code> 和
y
。之后,此模式匹配包含
: Tuple2[Int, Int]
,因此它尝试动态执行isInstanceOf[Tuple2[Int, Int]]
检查以查看是否对象还具有 Tuple2[Int, Int] 类型。但是,泛型类型信息在运行时会被删除,因此编译器会警告它实际上无法生成验证该对象是否已为类型参数[Int, Int]
实例化的代码。同样的,在下面的模式匹配中:
你会得到类似的警告。
You could rewrite your example to:
This pattern match actually consists of 2 matches - it now says: take the right hand side object of type
Tuple2[Int, Int]
and call the methodunapply[Int, Int]
on theTuple2
companion object. Theunapply[Int, Int]
will then verify that the object really has the typeTuple2
, and its result value will be used to bind values to variablesx
andy
.After that, this pattern match contains
: Tuple2[Int, Int]
, so it tries to do anisInstanceOf[Tuple2[Int, Int]]
check dynamically to see if the object additionally has the typeTuple2[Int, Int]
. However, generic type information is erased at runtime, so the compiler warns that it cannot actually produce code which verifies that the object is instantiated for type parameters[Int, Int]
.In the same way, in the following pattern match:
you would get a similar warning.
我认为你的问题的简短答案是:
因为
(Int, Int)
是不是类型,而(x: Int, y: Int)
code> 是有效的模式表达式。I think the short answer to your question is:
because
(Int, Int)
is not a type, while(x: Int, y: Int)
is a valid pattern expression.