关于此 Scala 模式匹配中未经检查的类型参数的警告?
此文件:
object Test extends App {
val obj = List(1,2,3) : Object
val res = obj match {
case Seq(1,2,3) => "first"
case _ => "other"
}
println(res)
}
给出此警告:
Test.scala:6: warning: non variable type-argument A in type pattern Seq[A]
is unchecked since it is eliminated by erasure
case Seq(1,2,3) => "first"
Scala 版本 2.9.0.1。
我不明白如何需要删除类型参数来执行匹配。第一个 case 子句旨在询问 obj 是否是一个包含 3 个元素等于 1、2 和 3 的 Seq。
如果我写了如下内容,我就会理解这个警告:
case strings : Seq[String] => ...
Why do I get the warning ,有什么好办法让它消失呢?
顺便说一句,我确实想与静态类型的对象进行匹配。在真实的代码中,我正在解析类似 Lisp 数据的东西 - 它可能是一个字符串、数据序列、符号、数字等。
This file:
object Test extends App {
val obj = List(1,2,3) : Object
val res = obj match {
case Seq(1,2,3) => "first"
case _ => "other"
}
println(res)
}
Gives this warning:
Test.scala:6: warning: non variable type-argument A in type pattern Seq[A]
is unchecked since it is eliminated by erasure
case Seq(1,2,3) => "first"
Scala version 2.9.0.1.
I don't see how an erased type parameter is needed to perform the match. That first case clause is meant to ask if obj is a Seq with 3 elements equal to 1, 2, and 3.
I would understand this warning if I had written something like:
case strings : Seq[String] => ...
Why do I get the warning, and what is a good way to make it go away?
By the way, I do want to match against something with static type of Object. In the real code I'm parsing something like a Lisp datum - it might be an String, sequence of datums, Symbol, Number, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是对幕后发生的事情的一些见解。考虑以下代码:
如果使用 scalac -Xprint:12 -unchecked 进行编译,您将在擦除阶段 (id 13) 之前看到代码。对于第一个类型模式,您将看到类似以下内容:
对于
Seq
提取器模式,您将看到类似以下内容:在这两种情况下,都有是一个类型测试,用于查看对象是否属于
Seq
类型(Seq[Int]
和Seq[A]
)。类型参数将在擦除阶段被消除。因此发出警告。尽管第二个可能是意外的,但检查类型确实有意义,因为如果对象不是Seq
类型,则该子句将不匹配,并且 JVM 可以继续执行下一个子句。如果类型匹配,则可以将对象强制转换为Seq
并调用unapplySeq
。RE: thoredge 对类型检查的评论。也许我们正在谈论不同的事情。我只是说:
翻译成这样:
使用类型检查,以便在转换完成时不会出现类转换异常。
类型模式 Seq[A] 中的警告非变量类型参数 A 因被擦除而被消除而未被检查是否合理,以及是否存在可能出现类转换异常的情况甚至 通过类型检查,我不知道。
编辑:这是一个例子:
Here is some insight to what happens behind the scene. Consider this code:
If you compile with
scalac -Xprint:12 -unchecked
, you'll see the code just before the erasure phase (id 13). For the first type pattern, you will see something like:For the
Seq
extractor pattern, you will see something like:In both cases, there is a type test to see if the object is of type
Seq
(Seq[Int]
andSeq[A]
). Type parameters will be eliminated during the erasure phase. Thus the warning. Even though the second may be unexpected, it does make sense to check the type since if object is not of typeSeq
that clause won't match and the JVM can proceed to the next clause. If the type does match, then the object can be casted toSeq
andunapplySeq
can be called.RE: thoredge comment on the type check. May be we are talking about different things. I was merely saying that:
translates to something like:
The type check is used so that when the cast is done there is no class cast exception.
Whether the warning non variable type-argument A in type pattern Seq[A] is unchecked since it is eliminated by erasure is justified and whether there would be cases where there could be class cast exception even with the type check, I don't know.
Edit: here is an example:
在外部声明匹配对象至少可以使其消失,但我不确定为什么:
Declaring the match object outside at least makes it go away, but I'm not sure why: