Ich zeichne eine leere auf, wie das folgende ohne Betrug und Verwendung asInstanceOf
erreichen.Pattern-Matching abhängigen Typen - wie AsInstanceOf zu vermeiden?
Angenommen, ich habe einen willkürlich versiegelten Objekttyp mit jeweils eigenen Typmitgliedern.
sealed trait Part { type A }
case object P1 extends Part { override type A = String }
case object P2 extends Part { override type A = Int }
Nun sage ich ein P und ein P.A Wert zusammen bündeln ...
trait PartAndA {
val p: Part
val a: p.A
}
object PartAndA {
type Aux[P <: Part] = PartAndA {val p: P}
def apply(_p: Part)(_a: _p.A): Aux[_p.type] =
new PartAndA {
override val p: _p.type = _p
override val a = _a
}
}
Wie kann ich sicher die folgende Erschöpfung erreichen Kontrolle und ohne manuelle Abgüsse?
def fold[A](pa: PartAndA)(p1: PartAndA.Aux[P1.type] => A,
p2: PartAndA.Aux[P2.type] => A): A =
pa.p match {
case P1 => p1(pa.asInstanceOf[PartAndA.Aux[P1.type]])
case P2 => p2(pa.asInstanceOf[PartAndA.Aux[P2.type]])
}
Ah ja, das wäre der Fall, wenn ich auf 'pa' passe, aber' pa.p' ist konkret und wir können darauf ohne Löschprobleme abgleichen. Ich weiß einfach nicht, wie man einen Beweis baut, der sagt, wenn ich den 'pa.p' Typ kenne, dann kenne ich den' PartAndA.Aux' Typ. – Golly