首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Scala的流擦除警告

Scala的流擦除警告
EN

Stack Overflow用户
提问于 2012-10-07 06:01:44
回答 1查看 506关注 0票数 3

有人能解释一下为什么这会给出擦除警告吗?

代码语言:javascript
复制
def optionStreamHead(x: Any) =
  x match {
    case head #:: _ => Some(head)
    case _ => None
  }

提供:

代码语言:javascript
复制
warning: non variable type-argument A in type pattern scala.collection.immutable.Stream[A] is unchecked since it is eliminated by erasure
            case head #:: _ => Some(head)

我意识到我可以编写这个案例if (x.isInstanceOf[Stream[_]]) ...而不得到警告,但在我的案例中,我实际上希望使用模式匹配,并且有一大堆我不理解的警告看起来很糟糕

这里有一个同样令人费解的案例:

代码语言:javascript
复制
type IsStream = Stream[_]

("test": Any) match {
  case _: Stream[_] => 1 // no warning
  case _: IsStream => 2 // type erasure warning
  case _ => 3
}
EN

Stack Overflow用户

回答已采纳

发布于 2012-10-07 06:41:46

这两个都是2.9中的bug,在2.10中得到了解决。在2.10中,我们得到了一个新的模式匹配引擎(称为virtpatmat,用于虚拟模式匹配器):

代码语言:javascript
复制
scala> def optionStreamHead(x: Any) =
  x match {
    case head #:: _ => Some(head)
    case _ => None
  }
optionStreamHead: (x: Any)Option[Any]

scala> optionStreamHead(0 #:: Stream.empty)
res14: Option[Any] = Some(0)

scala> ("test": Any) match {
     |   case _: Stream[_] => 1 // no warning
     |   case _: IsStream => 2 // type erasure warning
     |   case _ => 3
     | }
<console>:11: warning: unreachable code
                case _: IsStream => 2 // type erasure warning
                                    ^
res0: Int = 3
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12764270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档