无法递归地构建无形HList

时间:2017-10-04 19:28:00

标签: scala shapeless

我写了这段代码

object HList1Test extends App {
   import shapeless.{HList, HNil}
   import shapeless.syntax._
   import shapeless.ops._
   def myFunc(list: List[Int], result: HList = HNil) : HList = {
      def double (i: Int) : HList = if (i % 2 == 0) 2 * i :: HNil else { (2 * i).toString :: HNil}
      list match {
         case h :: t => {
            myFunc(t, double(h) ::: result)
         }
         case Nil => result
      }
   }
   println(myFunc(List(1, 2, 3)))
}

但我收到错误

Error:(16, 33) could not find implicit value for parameter prepend: shapeless.ops.hlist.Prepend[shapeless.HList,shapeless.HList]
            myFunc(t, double(h) ::: hlist)
Error:(16, 33) not enough arguments for method :::: (implicit prepend: shapeless.ops.hlist.Prepend[shapeless.HList,shapeless.HList])prepend.Out.
Unspecified value parameter prepend.
            myFunc(t, double(h) ::: hlist)

我的最终目标是让HList与“2”:: 4 ::“6”:: HNil

1 个答案:

答案 0 :(得分:4)

2 * i :: HNil(2 * i).toString :: HNil只有HList(而不是Int :: HNilString :: HNil相应的类型)太粗糙了。

由于double实际上会返回类型Int :: HNilString :: HNil的值,具体取决于其输入参数的值,doublePolymyFunc也是如此。

  object double extends Poly1 {
    implicit def evenCase[N <: Nat](implicit 
      mod: Mod.Aux[N, _2, _0], 
      toInt: ToInt[N]): Case.Aux[N, Int :: HNil] =
      at(n => 2 * toInt() :: HNil)

    implicit def oddCase[N <: Nat](implicit 
      mod: Mod.Aux[N, _2, _1], 
      toInt: ToInt[N]): Case.Aux[N, String :: HNil] =
      at(n => (2 * toInt()).toString :: HNil)
  }

  object myFunc extends Poly2 {
    implicit def hNilCase[R <: HList]: Case.Aux[HNil, R, R] = at((_, r) => r)

    implicit def hConsCase[H <: Nat, T <: HList, 
                           dblH <: HList, R <: HList, 
                           P <: HList, Out <: HList](implicit
      dbl: double.Case.Aux[H, dblH],
      prepend: Prepend.Aux[dblH, R, P],
      myF: myFunc.Case.Aux[T, P, Out]): Case.Aux[H :: T, R, Out] = 
      at { case (h :: t, r) => myFunc(t, double(h) ::: r) }
  }

  println(myFunc(_1 :: _2 :: _3 :: HNil, HNil)) //"6" :: 4 :: "2" :: HNil