如何派生作用于特定子类型的HList的无形op

时间:2019-06-27 13:40:41

标签: scala shapeless

我试图创建一个无形状的操作,该操作作用于给定类型的对象的HList。但是,我不知道如何使它在作为该对象子类型的对象的HList上工作。这是一个示例:

    trait Transform[I, O] { def f: I => O }

    trait Transformer[I, TH <: HList] {
      type Out <: HList
      def transform(input: I, transforms: TH): Out
    }
    object Transformer {
      type Aux[I, Transforms <: HList, Out0] = Transformer[I, Transforms] { type Out = Out0 }

      def apply[I, Transforms <: HList](implicit transformer: Transformer[I, Transforms]): Aux[I, Transforms, transformer.Out] = transformer

      implicit def hnilTransformer[I]: Aux[I, HNil, HNil] =
        new Transformer[I, HNil] {
          type Out = HNil
          override def transform(input: I, transforms: HNil): HNil = HNil
        }

      implicit def hconsTransformer[I, O, TIn <: HList, TOut <: HList]
        (implicit t: Transformer.Aux[I, TIn, TOut]): Aux[I, Transform[I, O] :: TIn, O :: TOut] =
          new Transformer[I, Transform[I, O] :: TIn] {
            type Out = O :: TOut
            override def transform(input: I, transforms: Transform[I, O] :: TIn): Out = {
              transforms.head.f(input) :: t.transform(input, transforms.tail)
            }
          }
    }

    def applyTransforms[I, TH <: HList](transforms: TH)(input: I)
                                       (implicit transformer: Transformer[I, TH]): transformer.Out = {
      transformer.transform(input, transforms)
    }

    val double = new Transform[Int, Int] { def f = _ * 2 }
    val int2Str = new Transform[Int, String] { def f = _.toString }

    applyTransforms(double :: int2Str :: HNil)(4) // shouldBe 8 :: "4" :: HNil

    trait SubTransform[I, O] extends Transform[I, O]
    val doubleSub = new SubTransform[Int, Int] { def f = _ * 2 }
    val int2StrSub = new SubTransform[Int, String] { def f = _.toString }

    applyTransforms(doubleSub :: int2StrSub :: HNil)(4) // fails to compile

第一个applyTransforms编译成功,但是第二个失败,并显示以下错误:

ShapelessSpec.scala:124: could not find implicit value for parameter transformer: Transformer[Int,shapeless.::[SubTransform[Int,Int],shapeless.::[SubTransform[Int,String],shapeless.HNil]]]
    applyTransforms(doubleSub :: int2StrSub :: HNil)(4)

我怀疑问题出在hconsTransformer上,但我不知道为什么这不应该在SubTransform范围内。

1 个答案:

答案 0 :(得分:1)

如果您希望此递归步骤与Transform的子类型一起使用,请修改

implicit def hconsTransformer[I, O, TIn <: HList, TOut <: HList, X]
(implicit t: Transformer.Aux[I, TIn, TOut], ev: X <:< Transform[I, O]): Aux[I, X :: TIn, O :: TOut] =
  new Transformer[I, X :: TIn] {
    type Out = O :: TOut
    override def transform(input: I, transforms: X :: TIn): Out = {
      transforms.head.f(input) :: t.transform(input, transforms.tail)
    }
  }

或使TransformerTH互变

trait Transformer[I, -TH <: HList] { ...