0

Consider the following:

sealed trait baseData {
  def weight: Int
  def priority: Int
} 

case class data1(override val weight: Int, override val priority: Int) extends baseData

How would I define a function with the following signature that transforms data1 into an HList?

def toHlist[A <: baseData] (data: A) = {}

I want to pass in a trait instance into the toHlist function instead of the actual case class because there will be more than one case class extending the trait. I also don't want to hardcode any fields; I'm looking for a totally generic solution.

I'm sure this is doable with the Shapeless library, but haven't been able to figure out how.

EDIT

toHList needs to be able to handle a baseData pointer to a case class instance, as so:

val data: baseData = data1(1,2)
toHlist(data) 

The reason for this is that we will have more than one case class extending baseData, and we will now know which one to pass to toHlist until run time.

Cigogne Eveillée
  • 2,178
  • 22
  • 36

1 Answers1

0

This probably isn't the final answer, but it will help you refine your question,

scala> def toHlist[A <: baseData](data: A)(implicit gen: Generic[A]): gen.Repr = gen.to(data)
toHlist: [A <: baseData](data: A)(implicit gen: shapeless.Generic[A])gen.Repr

scala> toHlist(data1(23, 13))
res0: shapeless.::[Int,shapeless.::[Int,shapeless.HNil]] = 23 :: 13 :: HNil
Miles Sabin
  • 23,015
  • 6
  • 61
  • 95