shapeless: Derive a Tupler for an HList

131 Views Asked by At

I'm very new to shapeless and struggling to derive a Tupler for my HList. Here is a simplified example:

object Builder {
  import shapeless.ops.hlist.Tupler.hnilTupler
  val empty: Builder[HNil] = new Builder[HNil](HNil)
}

class Builder[L <: HList: Tupler](list: L) {
  def add[T](value: T): Builder[T :: L] = new Builder[T :: L](value :: list)
  def tuple: Tupler[L]#Out = list.tupled
}

The line def add[T](value: T): Builder[T :: L] = new Builder[T :: L](value :: list) doesn't compile since there is no implicit Tupler[T :: L] and I cannot figure out to derive the next one from the previous one. Any thoughts?

1

There are 1 best solutions below

0
Dmytro Mitin On

shapeless.ops.hlist.Tupler.hnilTupler is not supposed to be imported.

Normally in Scala, if a method lacks some implicit, you add corresponding implicit parameter. Try

def add[T](value: T)(implicit tupler: Tupler[T :: L]): Builder[T :: L] = new Builder[T :: L](value :: list)