The following code example shows the core of my question:
// This is the base trait that the classes are extending
trait Operation[T] {
def operate(x: T): T
}
// Here are 2 case classes that exist for the sole purpose of being
// the generic type for the classes I'm making
case class CaseClass1(field1_1: String, field1_2: String, field1_3: String)
case class CaseClass2(field2_1: String, field2_2: String, field2_3: String)
// These are the 2 classes that extend my basic trait, implementing the operate
// method with some kind of logic
class FillField1 extends Operation[CaseClass1] {
def operate(input: CaseClass1): CaseClass1 = input.copy(field1_3 = "haha")
}
class FillField2 extends Operation[CaseClass2] {
def operate(input: CaseClass2): CaseClass2 = input.copy(field2_2 = "hoho")
}
import scala.reflect.runtime.universe._
// This is a function that prints out the typetag information currently available
def someFunc[T: TypeTag](x: Operation[T]): Unit = {
println(s"TypeTag is: ${typeOf[T]}")
}
// Here I'm creating a sequence of Operations, but they can have any generic
// type parameter
val someSeq: Seq[Operation[_]] = Seq(new FillField1, new FillField2)
someSeq.map(someFunc(_))
/*
Output:
TypeTag is: _$1
TypeTag is: _$1
*/
someFunc(new FillField1)
/*
Output:
TypeTag is: CaseClass1
*/
As you can see, when I call someFunc(new fillField1) I can properly find my typetag at runtime. But when I'm using someSeq, which is a Sequence that can contain multiple types of classes I can't get the typetag I need at runtime. Is this because you lose that information at runtime?
How can I get the proper typetag at runtime? So how could I get as output TypeTag is: CustomClass1 and TypeTag is: CustomClass2 when I'm using that Seq[Operation[_]]?
I'm working on an Apache Spark project where we have a structure similar to this and when I'm using that sequence I'm getting an issue that the TypeTag points to an unknown class, _$10 (or whatever name the compiler made for my typetag), instead of the actual TypeTag which would be CustomClass1 or CustomClass2...