When I run the following code snippet with scala
import scala.language.reflectiveCalls
def foo(a: Option[Any]): Option[Any] = {
a.filter {
case x: { def bar: Boolean } => x.bar
}
}
object Bar {
def bar: Boolean = true
}
println(foo(Some(Bar)))
I get a warning
warning: a pattern match on a refinement type is unchecked
I've tried the following:
@unchecked case x: { def bar: Boolean } => x.bar
case (@unchecked x): { def bar: Boolean } => x.bar
case (x @unchecked): { def bar: Boolean } => x.bar
case x: @unchecked { def bar: Boolean } => x.bar
case x: { def bar: Boolean } @unchecked => x.bar
case (x: { def bar: Boolean } @unchecked) => x.bar
case x: ({ def bar: Boolean } @unchecked) => x.bar
case x: { def bar: Boolean } => (x @unchecked).bar
case x: { def bar: Boolean } => (x: { def bar: Boolean } @unchecked).bar
None of those work. This also doesn't work:
a.filter { any => (any: @unchecked) match {
case x: { def bar: Boolean } => x.bar
}}
How do I suppress this warning?
Somewhat related links
This answer seems to use the @unchecked
successfully inside Some(...)
, but I don't see how to use it with filter
.
An additional pair of round parentheses around the { def ... }
is required:
case x: ({ def bar: Boolean }) @unchecked => x.bar
With the additional parentheses, it compiles just fine, without any warnings.
This seems to be similar to the syntax for the "classical type-lambdas", where
({ type Lam[X] = Foo[X] })#Lam
worked, whereas
{ type Lam[X] = Foo[X] }#Lam
didn't.