I am performing a repetitive task of creating a object that has same internal contents and hence thought of creating a generic method that would help me achieve this.
The internal object is as follows
case class Data(value: Int)
I have a base trait as follows
trait Base
There are a couple of classes that extend this trait
case class A(data: Data) extends Base
case class B(data: Data) extends Base
case class C(data: Data) extends Base
The generic method that I am trying to write to create the object
def getObject[T <: Base](data: Data, t: T) = {
T(data)
}
However, while trying to do so, I get a compile-time error saying that
Cannot resolve symbol T
Can you please let me know what I am missing in this method implementation. Note:- This is a very simplistic implementation of what I am trying to do in my code.
Consider typeclass solution for compile-time safety