Propagating implicit expansion for a formless LeftFolder

I am trying to solve this problem and want to reset HList

using a function Poly2

.

Here's a standalone MWE (you can copy it to the REPL with shapeless 2.0 to reproduce the problem)

import shapeless._; import shapeless.record._

case class RowParser[T](value: T)

sealed trait ColParser[T, K <: Symbol] { val column: Witness.Aux[K] }
case class Nullable[T, K <: Symbol](column: Witness.Aux[K], parserF: String => RowParser[T]) extends ColParser[T, K]
case class NonNullable[T, K <: Symbol](column: Witness.Aux[K], parserF: String => RowParser[T]) extends ColParser[T, K]

val cols = NonNullable('col1, (_ => RowParser(42))) :: Nullable('col2, (_ => RowParser("foo"))) :: HNil

object toRecord extends Poly2 {
  implicit def colParser[C, T, K <: Symbol, LL <: HList](
    implicit ev: C <:< ColParser[T,K]
  ) = at[Tuple2[List[Int], LL], C] {
    case ((vals, rec), x) => (vals, field[K](vals.headOption) :: rec)
  }
}

cols.foldLeft((List(1,2), HNil))(toRecord)
// expected: Some(1) :: Some(1) :: HNil

      

I am getting this error:

error: divergent implicit extension for type shapeless.ops.hlist.LeftFolder [shapeless. :: [NonNullable [Int, this.T], shapeless. :: [Nullable [String, this.T], shapeless.HNil]], ( List [Int], shapeless.HNil.type), toRecord.type]

starting from the hnilLeftFolder method in the LeftFolder object cols.foldLeft ((List (1,2), HNil)) (toRecord)

Thus, it is obvious that the compiler can find multiple implications to create needs LeftFolder

foldLeft

.

I can't figure out how to sort and explicitly tell the compiler what I really want to get LeftFolder

which is using toRecord

.

+3


source to share





All Articles