scala - Pattern-matching & existential type -
why doesn't work? scala should understand in case of case hasints, outer.type <: hasints.type. what's workaround (without removing type member)?
sealed trait outer { type inner } case object hasints extends outer { override type inner = int } def id(outer: outer): outer.inner = outer match { case hasints => 0 } edit:
on otherhand, if change type member existential universal, compiles.
sealed trait outer[inner] case object hasints extends outer[int] def id[i](outer: outer[i]): = outer match { case hasints => 0 } why work in first case , not second? they're arguably identical (and indeed, in dotty be.)
dependent types. there no particular way compiler determine outer.inner when return int. error puts well:
scala> def id(outer: outer): outer.inner = | outer match { | case x@ hasints => 0 : hasints.inner | } <console>:15: error: type mismatch; found : hasints.inner (which expands to) int required: outer.inner case x@ hasints => 0 : hasints.inner ^ the compiler particularly knows int. method definition says returns outer.inner. statically dependent types, has no other information apart outer.inner, conveys nothing. example, below change work:
sealed trait outer { type inner >: int } or could:
sealed trait outer { type inner def f: inner } case object hasints extends outer { override type inner = int def f:inner = 23 } def id(outer: outer): outer.inner = outer.f i not sure if 1 must go towards dependent types difficult them right (they might removed in future versions once dotty comes in). if can, rather use outer[t]
Comments
Post a Comment