hellraiser hellraiser - 4 months ago 38
Scala Question

API for Shapeless Extensible Records

I am trying to learn Shapeless(2.3.2). I have created a very simple container for extensible records:

case class Records[L <: HList](ps: L) {

def get(k: Witness)(implicit selector: Selector[L, k.T]): selector.Out = selector(ps)

def rm[V, Out <: HList](k: Witness)(implicit remover: Remover.Aux[L, k.T, (V, Out)]) =
this.copy(ps = remover(ps)._2)

def upd[F](f: F)(implicit updater: Updater[L, F]) = this.copy(ps = updater(ps, f))

Now I am trying to realise API method like:

def upsert[T](k: String, v: T) = ???

All of my attemps are finished with compile errors like:

could not find implicit value for parameter updater: shapeless.ops.record.Updater[L,T with shapeless.labelled.KeyTag[k.type,T]]

Could you please help me to understand how to build such APIs(not tightened to shapeless) and how to gather information to solve such issues ?


I figured, I'd make my comment an answer, so that it can be accepted.

From my experience working with shapeless, when compiler complains about missing implicit value, the first thing to do is to try and provide it to the relevant function via an implicit parameter, especially if the type in question is something from shapeless. Shapeless provides lots of those implicits, and there are very good chance adding implicit parameter will fix such error.

In this case, add the Updater (not sure about type parameters, but they should be in line with those from upd) implicit parameter to the upsert function.