scala – 模式匹配依赖类型 – 如何避免asInstanceOf?
发布时间:2020-12-16 18:31:18 所属栏目:安全 来源:网络整理
导读:我在如何完成以下操作时没有作弊和使用asInstanceOf. 假设我有一些任意密封类型的对象,每个对象都有自己的类型成员. sealed trait Part { type A } case object P1 extends Part { override type A = String } case object P2 extends Part { override type
我在如何完成以下操作时没有作弊和使用asInstanceOf.
假设我有一些任意密封类型的对象,每个对象都有自己的类型成员. sealed trait Part { type A } case object P1 extends Part { override type A = String } case object P2 extends Part { override type A = Int } 现在说我将P和P.A值捆绑在一起…… 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 } } 如何通过耗尽检查和没有手动演员安全地完成以下操作? 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]]) } 解决方法
我认为你的问题与
jvm type erasure有关.没有它你的问题可以简化为:
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 } 根据我的知识,jvm类型擦除没有更短(比你的或typeTags/classTags)的解决方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 基于angularjs的下拉选择框封装
- vim-fugitive – 如何运行`git diff -staged`与Fugitive?
- angularjs select标签 ng-options ng-change学习
- 在webservice 中使用ArrayList 对象.
- scala – SBT 0.11 InputKey取决于其他任务
- 如何加速AngularJS应用程序?
- angularjs – 角度ui路由器中的三级嵌套路由
- unix – 如何自动查找和报告损坏的符号链接?
- bash – Docker中终端内线路的Docker数量
- tfs-power-tools – Shell扩展:在Windows资源管理器上下文