Nice and common way of working around type erasure in Scala is to use ClassTag
s.
They're usually passed as implicit parameters. Unfortunately, traits can't take constructor parameters, so the best that we can have is:
import scala.reflect.ClassTag
trait MyTrait[T <: Enum] {
val ttag: ClassTag[T]
def myMethod(name: String): T = {
MyJavaClass.staticMethod(name, ttag.runtimeClass.asInstanceOf[Class[T]])
}
}
Then, every class extending MyTrait
must be defined like this:
class MyClass[T <: Enum](/*your params, if any*/)(implicit val ttag: ClassTag[T]) extends MyTrait[T] {
/*your class body*/
}
Then, if you have some concrete enum MyEnum
, you can create instances of your class seamlessly:
new MyClass[MyEnum](/*your params, if any*/)