Reputation: 512
I wonder how can I preserve my type constraints while trying to workaround over 'Covariant type parameter in a wrong position problem' situation. Here is the code:
trait Converter[SourceType, +JobType <: ConverterJobType[SourceType]] {
def convert[JT >: JobType](job: JT):String = job.someMethodFromJobType()// method is not accessible here. I would like to use JobType but actually job is treated like type Any.
}
object Converter{
implicit object CSVConverter extends Converter[CSV, CSVConverterJobType]{
def converter....
}
}
I need covariance so that my implicit object could be looked up.
case class CSVConverterJobType(...) extends ConverterJobType[SourceType]
object Application {
def process[T](job: List[T])(implicit
converter:Converter[T,ConverterJobType[T]]) = {...}
val list:List[CSV] = ...
process(list)
}
In order to process
method to be able to find implicit.... I need to make second type parameter covariant. But then I'm not able to use actual type information in convert
method.
Any idea how to overcome this?
Upvotes: 0
Views: 109
Reputation: 810
JT
needs to be <: JobType
in order for job.someMethodFromJobType()
to work. If JT
is an arbitrary supertype of JobType
then it doesn't have a someMethodFromJobType()
method.
You should convert the second type argument to a type member, especially if there is only one JobType
per SourceType
(if not, the implicit search won't work anyway as it won't know which JobType
to choose):
trait Converter[SourceType] {
type JobType <: ConverterJobType[SourceType]
def convert...
}
object Converter{
implicit object CSVConverter extends Converter[CSV]{
type JobType = CSVConverterJobType
def convert....
}
}
Upvotes: 2