creating common function for case class lens shapeless - could not find implicit value for parameter mkLens: shapeless.MkSelectDynamicOptic

39 views Asked by At

I'm trying to create a common function to update my case class using shapeless lens: but this fails could not find implicit value for parameter mkLens: shapeless.MkSelectDynamicOptic

import shapeless._
case class MyClass(a: String, b:String, c:Boolean)

val cl = MyClass("test", "test", false)

def update[T](fieldName: String, value:T): MyClass = {
  val l = lens[MyClass].selectDynamic(fieldName)
  l.set(cl)(value)
}

def updateA(value:String): MyClass = {
  update[String]("a", "new test")
}

but this work

def updateB(value: String): MyClass = {
  val l = lens[MyClass].selectDynamic("b")
  l.set(cl)(value)
}

As I understand compiler does not get any implicit for type T. Is there any way to create my own implicit for that, or may be there is better way to modify classes using shapeless.

0

There are 0 answers