折叠提供的HList

时间:2018-05-18 16:42:28

标签: scala shapeless implicits circe scala-2.12

我知道我的问题可能是“XY问题”,所以这里简要回顾一下我想要实现的目标。 假设我有以下类型:

trait Provider[T] { def provide: T }

我希望能够将从该类型提供的多个值组合成一个(每个提供的值可以有不同的类型)。组合发生在程序的不同部分,让我们定义类型Combiner(尝试对我的问题应用shapeless):

trait Combiner[Out, Providers <: HList] { 
  def combine(providers: Providers): Out 
}

我想出了以下内容:

class ShapelessCombiner[Out, Providers <: HList, P](implicit
  zero: Out,
  folder: LeftFolder.Aux[Providers, Out, P, Out]
) extends Combiner[Out, Providers] {
  def combine(providers: Providers): Out = folder(folder, zero)
}

为了指定用于折叠的函数,我创建了辅助方法:

def shapelessCombiner[Out, Providers <: HList](
  fun: Poly
)(implicit
  zero: Out,
  folder: LeftFolder.Aux[Providers, Out, fun.type, Out]
) = new ShapelessCombiner[Out, Providers]

一切都很好,除了它不起作用。 我试图使用以下代码测试它:

implicit val zero = Json.obj()

//incorrect implementation, but compilation is important here
object poly extends Poly2 {
implicit def caseInt =
    at[Json, Provider[Int]]((j, p) => j deepMerge Json.fromInt(p.provide))
  implicit def caseString =
    at[Json, Provider[String]]((j, p) => j deepMerge Json.fromString(p.provide))
}
def hlistInstance: Provider[Int] :: Provider[String] :: HNil = ???
val directResult = hlistInstance.foldLeft(zero)(poly) //compiles
val combiner = shapelessCombiner[Json, Provider[Int] :: Provider[String] :: HNil](poly)//do not compile, LeftFolder.AUX not found

我的问题是:

a)我如何解决这个编译问题,或者

b)是否有不同/更好的方法来解决我的初始问题

修改

事实证明我的错误是由错误的导入引起的,我以某种方式导入tuple.LeftFolder而不是hlist.LeftFolder,愚蠢的错误

1 个答案:

答案 0 :(得分:1)

以下代码编译

  import io.circe.Json
  import shapeless.{::, HList, HNil, Poly, Poly2}
  import shapeless.ops.hlist.LeftFolder

  trait Provider[T] {
    def provide: T
  }

  trait Combiner[Out, Providers <: HList] {
    def combine(providers: Providers): Out
  }

  class ShapelessCombiner[Out, Providers <: HList, P](implicit
                                                      zero: Out,
                                                      folder: LeftFolder.Aux[Providers, Out, P, Out]
                                                     ) extends Combiner[Out, Providers] {
    def combine(providers: Providers): Out = folder(providers, zero)
  }

  def shapelessCombiner[Out, Providers <: HList](
                                                  fun: Poly
                                                )(implicit
                                                  zero: Out,
                                                  folder: LeftFolder.Aux[Providers, Out, fun.type, Out]
                                                ) = new ShapelessCombiner[Out, Providers, fun.type]

  implicit val zero = Json.obj()

  object poly extends Poly2 {
    implicit def caseInt =
      at[Json, Provider[Int]]((j, p) => j deepMerge Json.fromInt(p.provide))
    implicit def caseString =
      at[Json, Provider[String]]((j, p) => j deepMerge Json.fromString(p.provide))
  }
  def hlistInstance: Provider[Int] :: Provider[String] :: HNil = ???
  val directResult = hlistInstance.foldLeft(zero)(poly)
  val combiner = shapelessCombiner[Json, Provider[Int] :: Provider[String] :: HNil](poly)