Scalac gives the error found: scala.Boolean (false) required: java.lang.Boolean

The code below checks for basic authentication. Here resp is the unauthorized response 401. I check if the Authorization header is present, and if it is present, I check its value, otherwise I call 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 } 

When I compile it, it gives an error for the match { case false => resp } saying found: scala.Boolean(false) required: java.lang.Boolean . I am confused about why its handling of scala Boolean is different from java Boolean.

I noticed that at the beginning of the file was the line import java.lang._ (I do not know why). I commented on this, and in the code, instead of a warning, a warning is issued:

 warning: match may not be exhaustive. It would fail on the following input: true 

I think this is because I did not write case true . But what made the initial error, and why does this only happen with import java.lang._ ?

EDIT:

Here is a minimal example of a problem:

 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 } 
+7
scala boolean
source share
1 answer

Implicit conversions don't seem to work if patterns match.

Consider:

 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(xx) 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 } ^ 

Compare with:

 scala> val f: java.lang.Boolean = false f: Boolean = false scala> f.<TAB> asInstanceOf booleanValue compareTo isInstanceOf toString scala> f || true res21: Boolean = true 

implicit conversions worked here, but not here:

 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 } ^ 

I agree that this is rather inconsistent, but I doubt that it can be fixed without introducing a special shell for the language or adopting scalac somehow recognizes pattern matching, where all patterns belong to the same type, and try to find an implicit conversion to this type of. A asInstanceOf[Boolean] would be to explicitly cast asInstanceOf[Boolean] . Although it is strange that the following works fine:

 scala> "foobar".startsWith("foo") match { case true => 3 ; case false => 4 } res26: Int = 3 
+4
source share

All Articles