简体   繁体   English

foldleft HList上的“发散的隐式展开”错误

[英]'diverging implicit expansion'-error on foldleft HList

I am trying to do a type-calculation to build some nested structure of collections but I get a 'diverging implicit expansion for type shapeless.ops.hlist.LeftFolder'-error when I try to use a Map-like collection with a List[T] as key or value type. 我尝试进行类型计算以构建集合的某些嵌套结构,但是当我尝试将类似Map的集合用于List时,我得到了'shapeless.ops.hlist.LeftFolder类型的隐式扩展'错误。 T]作为键或值类型。 This nested structure works: 这种嵌套结构的工作原理是:

import shapeless._
import shapeless.ops.hlist._

object poly extends Poly2 {
  implicit def wrapMap[T] = at[T, Int]((end, t) => Map(t -> end))
  //other collection wrap functions
}

def foldToNestedCollections[T <: HList, Out](hl: T)(implicit lf: LeftFolder.Aux[T, Any, poly.type, Out]): Out = lf.apply(hl, 1)

foldToNestedCollections(2 :: HNil) //compiles
foldToNestedCollections(2 :: 3 :: HNil) //compiles

this structure gives the error: 这个结构给出了错误:

import shapeless._
import shapeless.ops.hlist._

object poly extends Poly2 {
  implicit def wrapMap[T] = at[T, Int]((end, t) => Map(List(t) -> end)) //or Map(t, List(end)) gives the same error
  //other collection wrap functions
}

def foldToNestedCollections[T <: HList, Out](hl: T)(implicit lf: LeftFolder.Aux[T, Any, poly.type, Out]): Out = lf.apply(hl, 1)

foldToNestedCollections(2 :: HNil) //compiles
foldToNestedCollections(2 :: 3 :: HNil) //does not compile 'diverging ...

Update: If I build a custom converter it works. 更新:如果我构建一个自定义转换器,它将起作用。 I build this using an the answer on an earlier question HList to nested Map . 我使用对嵌套Map的早期问题HList的答案来构建此文件。 Any suggestions why? 有什么建议吗?

import shapeless._
import shapeless.ops.hlist._

sealed trait MyLeftFolder[L <: HList, T] {
  type Out

  def convert(hlist: L, value: T): Out
}

object MyLeftFolder {

  type Aux[L <: HList, T, Out2] = MyLeftFolder[L, T] { type Out = Out2 }

  private trait Impl[L <: HList, T, Out2] extends MyLeftFolder[L, T] {
    override type Out = Out2
  }

  implicit def hnil[T]: Aux[HNil, T, T] = new Impl[HNil, T, T] {
    override def convert(hlist: HNil, value: T): T = value
  }

  implicit def hnil2[T]: Aux[HNil.type, T, T] = new Impl[HNil.type, T, T] {
    override def convert(hlist: HNil.type, value: T): T = value
  }


  implicit def recurseint[L <: HList, T](implicit inner: MyLeftFolder[L, T]): Aux[Int :: L, T, Map[List[Int], inner.Out]] = new Impl[Int :: L, T, Map[List[Int], inner.Out]] {
    override def convert(hlist: Int :: L, value: T): Map[List[Int], inner.Out] = {
      val im = inner.convert(hlist.tail, value)
      Map(List(hlist.head) -> im)
    }
  }
}

def foldToNestedCollections[T <: HList](hl: T)(implicit lf: MyLeftFolder[T, Any]): lf.Out = lf.convert(hl, 1)

foldToNestedCollections(2 :: HNil) //compiles
foldToNestedCollections(2 :: 3 :: HNil) //compiles

Use the following custom LeftFolder instead of standard shapeless.ops.hlist.LeftFolder . 使用下面的自定义LeftFolder而不是标准shapeless.ops.hlist.LeftFolder It differs in usage of Lazy . Lazy用法不同。

import shapeless.{::, DepFn2, HList, HNil, Lazy, Poly2}
import shapeless.poly.Case2

trait LeftFolder[L <: HList, In, HF] extends DepFn2[L, In] with Serializable

object LeftFolder {
  def apply[L <: HList, In, F](implicit folder: LeftFolder[L, In, F]): Aux[L, In, F, folder.Out] = folder

  type Aux[L <: HList, In, HF, Out0] = LeftFolder[L, In, HF] { type Out = Out0 }

  implicit def hnilLeftFolder[In, HF]: Aux[HNil, In , HF, In] =
    new LeftFolder[HNil, In, HF] {
      type Out = In
      def apply(l : HNil, in : In): Out = in
    }

  implicit def hlistLeftFolder[H, T <: HList, In, HF, OutH, FtOut]
    (implicit f : Case2.Aux[HF, In, H, OutH], ft : Lazy[LeftFolder.Aux[T, OutH, HF, FtOut]]): Aux[H :: T, In, HF, FtOut] =
    new LeftFolder[H :: T, In, HF] {
      type Out = FtOut
      def apply(l : H :: T, in : In) : Out = ft.value(l.tail, f(in, l.head))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM