scala - How to pattern match with dependent type without using it? -
this hard phrase, please, let me show example:
trait cache trait querylike { type result } trait query[a] extends querylike { type result = def exec: result } trait cachedquery[a] extends querylike { type result = 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 , runs fine pattern matching done on different types (query, cachedquery) instead of relying on generics this question.
but still compiler warning :
warning:(18, 12) abstract type result in type pattern a$a4.this.query[query.result] unchecked since eliminated erasure case q: query[query.result] => q.exec
since don't work on dependent type query.result directly in anyway (like casting different operations), it'd ideal erase , away warning. unfortunately, using wildcard doesn't work reason:
... case q: query[_] => q.exec // type mismatch case cq: cachedquery[_] => cq.execwithcache(cache) ... is there better way without generating compiler warning?
this error isn't specific path-dependent types. if tried match on query[a] same error, because type parameter a erased @ runtime. in case, it's not possible type parameter can other type you're looking for. since query[a] querylike { type result = a}, should query[query.result], though unusual way @ it. could use @unchecked annotation suppress warning, if wish:
def exec(query: querylike)(implicit cache: cache): query.result = query match { case q: query[query.result @unchecked] => q.exec case cq: cachedquery[query.result @unchecked] => cq.execwithcache(cache) } while it's tough if apply actual use-case, restructure code avoid matching entirely, , handle (possibly) more elegantly via polymorphism. since last exec requires implicit cache anyway, wouldn't seem hurt allow each querylike. api can more uniform way, , wouldn't need figure out method call.
trait cache trait querylike { type result def exec(implicit cache: cache): result } trait query[a] extends querylike { type result = } trait cachedquery[a] extends querylike { type result = } def exec(query: querylike)(implicit cache: cache): query.result = query.exec if query[a] requires exec without cache, provide overload dummyimplicit allow work without one.
def exec(implicit d: dummyimplicit): result
Comments
Post a Comment