将 `@unchecked` 放在哪里以抑制 "pattern match on a refinement type is unchecked"?

Where to put the `@unchecked` to suppress "pattern match on a refinement type is unchecked"?

当我 运行 下面的代码片段 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)))

我收到警告

warning: a pattern match on a refinement type is unchecked

我试过以下方法:

@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 这些作品。这也不起作用:

  a.filter { any => (any: @unchecked) match {
    case x: { def bar: Boolean } => x.bar
  }}

如何取消此警告?


有点相关的链接

似乎在 Some(...) 中成功使用了 @unchecked,但我不知道如何将它与 filter 一起使用。

{ def ... } 周围需要一对额外的圆括号:

case x: ({ def bar: Boolean }) @unchecked => x.bar

有了额外的括号,它编译得很好,没有任何警告。


这似乎与 "classical type-lambdas" 的语法相似,其中

({ type Lam[X] = Foo[X] })#Lam

有效,而

{ type Lam[X] = Foo[X] }#Lam

没有。