How do I eliminate type erasure warning?

主宰稳场 提交于 2019-12-20 05:01:00

问题


I have this code that gets the abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure warnings in every place that the case keyword is used. I have a feeling it's something strange about the reflection APIs. The thing is, it works when I run it (both passing and failing code paths) so it seems like the warning is erroneous. How do I eliminate the warning?

  import scala.reflect.runtime.{universe => u}

  val docs = {
    val ann = u.typeOf[T].members
      .collect { case m: u.MethodSymbol if m.isGetter => m }
      .find(_.name.decoded == prop.propertyName)

    val docAnnotation = ann.flatMap(_.annotations.find(_.tpe.typeSymbol.name.decoded == "docs"))
    val trees = docAnnotation.map(_.scalaArgs).getOrElse(Nil)
    val args = trees.map {
      case u.Apply(_, List(u.Literal(u.Constant(value)))) => Some(value.asInstanceOf[String])
      case u.Select(_, name) if name.decoded == "None" => None
    }
    val safeGetArg = args.lift(_: Int).flatten

    Documentation(safeGetArg(0), safeGetArg(1))
  }

回答1:


Eliminate the u, eliminate the warning?

apm@mara:~$ scala210
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.reflect.runtime.universe
import scala.reflect.runtime.universe

scala> import universe._
import universe._

scala> class X { val x = 7 ; var y = "hi" }
defined class X

scala> typeOf[X].members collect { case m: MethodSymbol => m }
res0: Iterable[reflect.runtime.universe.MethodSymbol] = List(method y_=, method y, value x, constructor X, method $asInstanceOf, method $isInstanceOf, method synchronized, method ##, method !=, method ==, method ne, method eq, method notifyAll, method notify, method clone, method getClass, method hashCode, method toString, method equals, method wait, method wait, method wait, method finalize, method asInstanceOf, method isInstanceOf, method !=, method ==)

scala> typeOf[X].members collect { case m: MethodSymbol if m.isGetter => m }
res1: Iterable[reflect.runtime.universe.MethodSymbol] = List(method y, value x)


来源:https://stackoverflow.com/questions/23274705/how-do-i-eliminate-type-erasure-warning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!