I am having problems converting between to case classes using LabelledGeneric
A simplified example of my code is below:
import shapeless._
import shapeless.record._
def removeKeys[
F <: Product,
T <: Product,
HF <: HList,
HT <: Product
](
from: F,
removeField: String
)(
implicit genericFrom: LabelledGeneric.Aux[F, HF],
genericTo: LabelledGeneric.Aux[T, HT]
): T = {
val hListFrom = genericFrom.to(from)
val hListTo = hListFrom - Witness(removeField) // Missing implicit Remover
genericTo.from(hListTo) // If I remove multiple fields in a say foldLeft how do I ensure the resulting HList is of type HT?
}
I am clearly missing a Remover - how should I conjure one up considering that I eventually want to foldLeft over a set of labels to remove hListFrom?
My ultimate intention is to select all the fields from case class F that exist on case class T with the same type.
For example, given:
case class A(a: Int, b: Double, c: Boolean)
case class B(b: Double, c: Boolean)
I want
def f[F <: Product, T <: Product](cc: F): T
so that f(A(1, 1.0, true))
returns B(1.0, true)
I guess I need the intersection of the two HLists however it has just occurred to me that I need to potentially reorder this depending on the order of the output case class constructor parameters.
I am enjoying learning Shapeless but there is quite a steep learning curve and many of the examples tend omit the details on how to ensure implicits are passed to generic code.
HT
in general won't uniquely identify aT
. This presumably isn't what you want, and there are ways to work around it, but if you gave some example inputs, outputs, and usage it'd be a lot clearer. – Travis BrownA
, in which casef(A(1, 1.0, true))
would be ambiguous. – Travis Browndef convert[FromType, ToType](instance: FromType): ToType
– Terry Dactyl