Scala: abstract type pattern A is unchecked since it is eliminated by erasure

耗尽温柔 提交于 2021-02-08 12:16:35

问题


I am writing the function that can catch exceptions of the certain type only.

def myFunc[A <: Exception]() {
    try {
        println("Hello world") // or something else
    } catch {
        case a: A => // warning: abstract type pattern A is unchecked since it is eliminated by erasure
    }
}

What is the corrent way to bypass jvm type erasure in such case?


回答1:


You could use ClassTag like in this answer.

But I'd prefer this approach:

def myFunc(recover: PartialFunction[Throwable, Unit]): Unit = {
  try {
    println("Hello world") // or something else
  } catch {
    recover
  }
}

Usage:

myFunc{ case _: MyException => }

Using ClassTag:

import scala.reflect.{ClassTag, classTag}

def myFunc[A <: Exception: ClassTag](): Unit = {
  try {
    println("Hello world") // or something else
  } catch {
    case a if classTag[A].runtimeClass.isInstance(a) =>
  }
}

Note also that in general you should use Try with recover method: Try will catch only NonFatal exceptions.

def myFunc(recover: PartialFunction[Throwable, Unit]) = {
  Try {
    println("Hello world") // or something else
  } recover {
    recover
  }.get // you could drop .get here to return `Try[Unit]`
}



回答2:


For every type check (e.g. case a: A) the JVM needs the corresponding class object to perform the check. In your case the JVM does not have the class object, because A is a variable type parameter. However, you can add additional information about A by implicitly passing a Manifest[A] to myFunc. As a shorthand you can just add : Manifest to your type declaration of A:

def myFunc[A <: Exception : Manifest]() {
    try {
        println("Hello world") // or something else
    } catch {
        case a: A => // warning: abstract type pattern A is unchecked since it is eliminated by erasure
    }
}


来源:https://stackoverflow.com/questions/21180973/scala-abstract-type-pattern-a-is-unchecked-since-it-is-eliminated-by-erasure

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