Im trying to construct a generic DAO that is instantiated with a concrete class of either type:
- Regular
- Snappy <: Regular
I currently have a trait DAO that describes the available methods, get/create/upsert. At compile time I would like to change/define each of the methods based on the type parameter i am passing. Here is some code:
trait Regular {
id: int
}
trait Snap extends Regular {
isSnappy: Boolean = true
}
trait DAO[T<: Regular] {
def get(id: String)(implicit x: Param,ct: ClassTag[T]): Future[Either[String, T]]
}
if the trait is Regular:
def get(id:String)(implicit x: Param,ct: ClassTag[T]): Future[Either[String, T]] = {
//Handle regular trait flow
}
if the trait is Snap <: Regular:
def get(id:String)(implicit x: Param,ct: ClassTag[T]): Future[Either[String, T]] = {
//Handle snappy
}
Both defs would return the same type T.
I want to be able to change the def at compile time, where I have knowledge of the Type parameter in the DAO. Then at run time I want to be able to instantiate the class but have it handle the def based on the type being passed.
Im not sure how to handle this in scala, whether its a macro based solution, overloaded methods, some type of implicit defined for the Dao. Any direction would be much appreciated! Im not sure if this is a unique question but I have read through as much as I could regarding compile time definition based on type parameters while still being able to refer to the same trait (DAO in this case)