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

标签 scala annotations warnings suppress-warnings 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

这些都不起作用。这也行不通:

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

如何抑制此警告?


有些相关的链接

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

最佳答案

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

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

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


这似乎类似于“经典类型 lambda”的语法,其中

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

有效,而

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

没有。

关于scala - 将 `@unchecked` 放在哪里以抑制 "pattern match on a refinement type is unchecked"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51615182/

相关文章:

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

java - DBSCAN 及其索引是否应该具有相同的距离函数

scala - 使用鹅毛笔时为 `exception during macro expansion: [error] scala.reflect.macros.TypecheckException`

java - 无法通过 ReflectionUtils.setField 设置字段值

java - 为什么 Spring Data 存储库方法参数名称在 Java 8 上也不可用?

c++ - 如何解决警告/usr/bin/ld : warning: libtiff. so.4,需要/home/user/libs/opencv/lib/libopencv_highgui.so,可能与libtiff.so.5冲突?

scala - 光滑的通用且与驱动程序无关

list - scala 列表 LineSegment

python - 如何避免python中指针属性的类型提示引起的循环依赖

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP