score:10
Accepted answer
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*/)
Source: stackoverflow.com
Related Query
- Scala trait runtime class from type parameter
- How do I get the runtime Class of a parameterized Type in a Scala trait
- How to bind a class that extends a Trait with a monadic type parameter using Scala Guice?
- Trait runtime type of type parameter through TypeTag when used with Existential type in Scala
- How convert from trait with type member to case class with type parameter an viceversa
- Scala - obtaining a class object from a generic type
- Any way to obtain a Java class from a Scala (2.10) type tag or symbol?
- How to access a Java static method from Scala given a type alias for that class it resides in
- Scala by Example - trait type parameter with context bounds mistake?
- What is the colon in the type parameter of a scala class
- How to cast a variable to certain runtime type got from TypeCast in Scala
- Scala error: type arguments do not conform to class type parameter bounds
- generic trait taking a class and it's companion object as a type parameter
- Scala path dependent return type from parameter
- Scala method where type of second parameter equals part of generic type from first parameter
- How to require at compile time that a type parameter be a trait (and not a class or other type value)?
- scala macro generic field of generic class not apply class generic type parameter
- In Scala, how do i tell an abstract base class that type parameter T supports implicit conversion from Int (or Float, or...)?
- Type Parameter in Case Class Using Trait w/ Implicits
- Valid type casting of both covariant and contravariant class at runtime in Scala
- scala - implementing trait method with parameter that is child of expected type
- Need to load a scala class at runtime from a jar and initialize it
- Scala instantiate a Concrete Class From Generic Type
- Prevent a Scala trait from qualifying as a SAM type
- Using type parameter in anonymous class in Scala
- Is there are way to create a generic class with a trait or mixin that is a subtype of the type parameter
- scala class extend a trait with generic which is a type of a field
- Good ways to create a Scala class whose Type parameter depends on composing object Type
- How can I access type members from a trait in Scala
- Cast a variable to runtime type from existential typed TypeCast in Scala
More Query from same tag
- Generic implementation von java.util.Comparator in Scala
- Scalatest: should be Matchers with extra description?
- How to get the row top 1 in Spark Structured Streaming?
- Scala File poller design
- Streaming Kmeans Spark JAVA
- Calling functions before they are defined (forward reference extends over definition of variable)
- way to copy data with new foreign key ids
- Refering to self type of a trait
- Scala Play Framework on Windows NoClassDefFoundError out of the box
- How to merge HDFS small files into a one large file?
- Play Framework 2 (Scala) templates: see if user is logged in
- Spark Streaming: how to propagate updates to a Broadcast variable to the whole cluster?
- scala macro Unknown type error with java bean class creation
- Retrieve all the classes annotated with a specific annotation(s)
- Apache Spark Object not Serializable Exception for json parser
- Error in scala IDE
- what good orm api will work well with scala or erlang
- MultipartFormData(Map(filename -> List(abcd)),List(),List(),List()) scala
- Scala - Executing every element until they all have finished
- StringContext$InvalidEscapeException: invalid escape '\:' not one of [\b, \t, \n, \f, \r, \\, \", \'] when creating an HTML string body
- How to replace all the numbers with literal \d in scala?
- Compile error on transposing a dense matrix in Spark MLlib
- Scala unapplySeq extractor syntax
- value collectAsMap is not a member of org.apache.spark.rdd.RDD
- Output contents of DStream in Scala Apache Spark
- A bigger loop in Scala
- How to convert Option[List[String]] to List[String] in Scala?
- How to write streaming data to S3?
- What are the advantages or disadvantages of declaring function/method in companion objects versus declaring them in traits?
- Spark Scala CSV Input to Nested Json