Is it possible to simplify this scala match statement using @ syntax?

Cre*_*eos 3 scala

Is it possible to simplify the following match statement using @ syntax?

foo match {
  case f: Foo => y(f)
  case f if forceY => y(f)
  case _ => x
}
Run Code Online (Sandbox Code Playgroud)

哪里forceYboolean

我尝试了以下操作,但遇到了编译错误,对于编译器来说,它确实看起来像是可疑的语法。也许这无法表达?

foo match {
  case f @(_: Foo | _ if forceY) => y(f)
  case _ => x
}
Run Code Online (Sandbox Code Playgroud)

Kot*_*lar 6

foo match {
  case f if f.isInstanceOf[Foo] || forceY => y(f)
  case _ => x
}
Run Code Online (Sandbox Code Playgroud)

您不能@专门使用,因为语法f: Foo只能出现在的匹配部分中case,而不能出现在条件(之后if)中。

原始版本也不错。如果您实际在右侧的长度大于just y,则可以y显式定义并保留这三种情况,因为它们看起来很好。