This is hard to phrase, so please, let me show an example:
trait Cache
trait QueryLike {
type Result
}
trait Query[A] extends QueryLike {
type Result = A
def exec: Result
}
trait CachedQuery[A] extends QueryLike {
type Result = A
def execWithCache(cache: Cache): Result
}
def exec(query: QueryLike)(implicit cache: Cache): query.Result = query match {
case q: Query[query.Result] => q.exec
case cq: CachedQuery[query.Result] => cq.execWithCache(cache)
}
This compiles and runs fine as pattern matching is done on different types (Query
, CachedQuery
) instead of relying on generics like this question.
But I still get compiler warning like :
Warning:(18, 12) abstract type Result in type pattern A$A4.this.Query[query.Result] is unchecked since it is eliminated by erasure case q: Query[query.Result] => q.exec
Since I don't work on dependent type query.Result
directly in anyway (like casting it for different operations), it'd be ideal to just erase it completely and do away with the warning. But unfortunately, using wildcard doesn't work for a good reason:
...
case q: Query[_] => q.exec // type mismatch
case cq: CachedQuery[_] => cq.execWithCache(cache)
...
Is there a better way to do this without generating compiler warning?