键入安全型组合,无形

时间:2016-12-30 11:41:38

标签: scala generic-programming shapeless

拥有以下代码:

import shapeless.{::, Generic, HList, HNil, Lazy}

object Problem {
  trait In {
    def bar: Double
  }

  trait A {
    def foo: Int
    type I <: In
  }

  /////////////////////////////////////////////////////

  final case class A1In(d: Double) extends In {
    override def bar: Double = 1.1 + d
  }

  final case class A1() extends A {
    override def foo: Int = 1
    override type I = A1In
  }

  final case class A2In(d: Double) extends In {
    override def bar: Double = 1.1 + d
  }

  final case class A2() extends A {
    override def foo: Int = 1
    override type I = A2In
  }


  final case class AListIn[T <: HList](items: T)(implicit ev: isIn[T]) extends In {
    override def bar = 1.1
  }

  final case class AList[T <: HList](items: T)(implicit ev: isA[T]) extends A {
    override def foo: Int = 555
    override type I = AListIn[???]
  }

  trait isA[T] {
    def aux_foo(value: T): Int
  }

  trait isIn[T] {
    def aux_bar(value: T): Double
  }
  /////////////////////////////////////////////////////

  def alloc(a: A): In = ????

  def usage() = {
    val a1: A1 = A1() 
    val a2: A2 = A2() 

    val l: AList[::[A1, ::[A2, ::[A1, HNil]]]] = AList(a1 :: a2 :: a1 :: HNil) 

    val a1In: A1In = A1In(1.2) 
    val a2In: A2In = A2In(9.3) 

    val lIn: AListIn[::[A2In, ::[A1In, HNil]]] = AListIn(a2In :: a1In :: HNil)
  }

}

如何修复它以便按预期工作? 例如,如何使用正确的类型代替???,这是应用HList类型映射的正确类型isA -> isIn。映射必须遵循A -> In中定义为type I <: In的{​​{1}}映射的自然关联 以及如何实现trait A函数,对于alloc的任何具体实例,它将生成In的相应实例? A的具体实现应该是相应In s的路径依赖类型吗?

以下A的代码isA的代码类似

isIn

0 个答案:

没有答案