Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 222
tmporaries Avatar asked Jan 17 '14 08:01

tmporaries


1 Answers

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]` } 
like image 127
senia Avatar answered Nov 13 '22 20:11

senia