简体   繁体   中英

Pattern matching dependent types - how to avoid asInstanceOf?

I'm drawing a blank on how to accomplish the following without cheating and using asInstanceOf .

Say I have some arbitrary sealed type of objects, each with their own type members.

  sealed trait Part { type A }
  case object P1 extends Part { override type A = String }
  case object P2 extends Part { override type A = Int }

Now say I bundle a P and a PA value together...

  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
      }
  }

How can I safely accomplish the following with exhaustion checking and without manual casts?

  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]])
    }

I think your problem is connected with jvm type erasure . Without it your problem could be simplified to:

sealed trait Part { type A }
case class P1() extends Part { override type A = String }
case class P2() extends Part { override type A = Int }

trait PartAndA[P <: Part] {
  val p: P
  val a: p.A
}

object PartAndA {
  type Aux[P <: Part] = PartAndA[P]

  def apply(_p: Part)(_a: _p.A): PartAndA[_p.type] =
    new PartAndA[_p.type] {
      override val p: _p.type = _p
      override val a          = _a
    }
}

def fold[A, T: ClassTag](pa: PartAndA[T])(p1: PartAndA[P1] => A,
                          p2: PartAndA[P2] => A): A =
  pa match {
    case s: PartAndA[P1]  => p1(pa) // here P1 is lost, err 
    case i: PartAndA[P2] => p2(pa) // here P2 is lost, err
  }

According to my knowledge there is no shorter (than yours or with typeTags/classTags ) workaround of jvm type erasure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM