I have external service which returns AnyRef I want to cast result to Boolean, or return false if return is not Boolean.
Here is a example:
object ExternalService {
def sendEvent(s: String) : AnyRef = {
return true }
}
object Caller {
def isValid(s: String): Boolean = {
val value = ExternalService.sendEvent("test")
value match {
// pattern type is incompatible with expected type found Boolean expected AnyRef
case b: Boolean => b
case _ => false
}
}
}
but i got
Error:(6, 12) the result type of an implicit conversion must be more specific than AnyRef return true; Error:(6, 12) type mismatch; found : Boolean(true) required: AnyRef return true; Error:(17, 15) pattern type is incompatible with expected type; found : Boolean required: Object case b: Boolean => b
How can I cast AnyRef to Boolean in this case or in general?
java.lang.Boolean
instead - unlike scala's version, that doesn't inherit fromAnyVal
so should work. – Astrid