Scala:AnyVal推断而不是InputStream

时间:2016-01-30 17:27:58

标签: scala types

非常简单的Scala 2.11.7功能我不知道,为什么有类型
推理错误:

a type was inferred to be `AnyVal`; this may indicate a programming error.
def isWrapper(is: FileInputStream): InputStream = {
                                                ^

虽然PushbackInputStream - > FilterInputStream - > InputStreamGZIPInputStream - > InflaterInputStream - > FilterInputStream - > InputStream

def isWrapper(is: FileInputStream): InputStream = {
  val pb = new PushbackInputStream(is, 2)
  val signature = new Array[Byte](2)
  pb.read(signature)
  pb.unread(signature)
  if (signature.sameElements(Array(0x1F, 0x8B))) {
    new GZIPInputStream(new BufferedInputStream(pb))
  } else {
    pb
  }
}

错误表示评估if块的结果不能是InputStream。我为什么出错?如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

就是这个LUB:

scala> (new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))
<console>:12: warning: a type was inferred to be `AnyVal`; this may indicate a programming error.
       (new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))
                                              ^
res1: Boolean = false

查看文档中的“完整”签名,以查看有害的B >: A界限。

不确定为什么插入符号是关闭的。