gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:31:53 27 4
gpt4 key购买 nike

下面的代码检查基本身份验证。这里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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com