Scalac 给出错误发现 : scala. Boolean(false) required : java. lang.Boolean

标签 scala boolean

下面的代码检查基本身份验证。这里resp是 401 未经授权的响应。我检查是否 Authorization header 存在,如果存在,我验证它的值,否则我调用 resp :

def validate(authHeader: String): Boolean = {
   //........
} 
val authHeader = Option(request.getHeader("Authorization"))
authHeader match {
  case Some(header) if header.startsWith("Basic ") => validate(header) match { case false => resp }
  case _ => resp
}

当我编译它时,它给出了行 match { case false => resp } 的错误说 found: scala.Boolean(false) required: java.lang.Boolean .我很困惑为什么它处理 scala Boolean 与 java Boolean 不同。

我注意到有一行 import java.lang._在文件的开头(我不知道为什么)。我将其注释掉,代码给出警告而不是错误:
warning: match may not be exhaustive.
It would fail on the following input: true

我想这是因为我没有写case true .但是是什么导致了最初的错误发生,为什么它只发生在 import java.lang._ 上?

编辑:

这是该问题的一个最小示例:
val f: java.lang.Boolean = false
val f2: scala.Boolean = false

/* The following line produces this error:
error: type mismatch;
 found   : scala.Boolean(false)
 required: java.lang.Boolean
*/
f match { case false => 5 }

/* The following line produces this warning:
warning: match may not be exhaustive.
It would fail on the following input: true
*/
f2 match { case false => 5 }

最佳答案

在模式匹配的情况下,似乎隐式转换无效。

考虑:

scala> case class Foo(x: Int)
defined class Foo

scala> case class Bar(x: Int)
defined class Bar

scala> implicit def foo2bar(x: Foo) = Bar(x.x)
foo2bar: (x: Foo)Bar

scala> Foo(3) match { case Foo(3) => 3; case _ => 4 }
res19: Int = 3

scala> Foo(3) match { case Bar(3) => 3; case _ => 4 }
<console>:14: error: constructor cannot be instantiated to expected type;
 found   : Bar
 required: Foo
              Foo(3) match { case Bar(3) => 3; case _ => 4 }
                                  ^

与之比较:
scala> val f: java.lang.Boolean = false
f: Boolean = false

scala> f.<TAB>
asInstanceOf   booleanValue   compareTo      isInstanceOf   toString       

scala> f || true
res21: Boolean = true

隐式转换在这里有效,但在这里无效:
scala> f match { case false => 3; case true => 4 }
<console>:15: error: type mismatch;
 found   : scala.Boolean(false)
 required: java.lang.Boolean
              f match { case false => 3; case true => 4 }
                             ^
<console>:15: error: type mismatch;
 found   : scala.Boolean(true)
 required: java.lang.Boolean
              f match { case false => 3; case true => 4 }
                                              ^

我同意这是非常违反直觉的,但我怀疑它可以在不为语言引入特殊大小写或制作 scalac 的情况下修复。以某种方式识别所有模式都属于单一类型的模式匹配,并尝试找到到该类型的隐式转换。解决方法是执行显式 asInstanceOf[Boolean] throw 。尽管以下工作正常很奇怪:
scala> "foobar".startsWith("foo") match { case true => 3 ; case false => 4 }
res26: Int = 3

关于Scalac 给出错误发现 : scala. Boolean(false) required : java. lang.Boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497984/

相关文章:

javascript - 将复选框绑定(bind)到 boolean 变量

scala - Spark : Draw learning curve of a model with spark

sql - 根据 Postgres 中的另一列设置列的值?

c# - 这是“切换” boolean 值的可接受方法吗?

php - 在PHP中打印 boolean 值

列表上的 Python boolean 运算 - 不一致的结果

function - Scala通用函数值(匿名函数)-缺少参数类型(错误)

scala - Play 2.5 + Slick + DI 问题

java - 如何在 Java 中从 Scala 传递通过包对象定义的隐式 val

scala - 重载方法应使用替代方法 :