Is it possible, using Shapeless, to extract a value of a specific type from the case class? So far I can do this:
def fromCaseClass[T, R <: HList](value: T)(implicit ga: Generic.Aux[T, R]): R = {
ga.to(value)
}
Which then allows me to retrieve the values procedurally:
scala> case class ServiceConfig(host: String, port: Int, secure: Boolean)
defined class ServiceConfig
scala> val instance = ServiceConfig("host", 80, true)
instance: ServiceConfig = ServiceConfig(host,80,true)
scala> fromCaseClass(instance).select[Boolean]
res10: Boolean = true
scala> fromCaseClass(instance).select[Int]
res11: Int = 80
However, when I try to write a function to do this, I become attached to implications that were not found:
def getByType[C, X](value: C)(implicit ga: Generic.Aux[C, X]): X = {
fromCaseClass(value).select[X]
}
<console>:12: error: could not find implicit value for parameter ga: shapeless.Generic.Aux[C,R]
fromCaseClass(value).select[X]
Presumably, I get this because the compiler cannot verify that my parameter is not a case class. Is there any way to do this?
I am completely new to Shapeless, so I'm not quite sure if I am trying to do something crazy or miss something simple.
Update
I feel like I'm getting a little closer. I can implement like this:
def newGetByType[C, H <: HList, R]
(value: C)
(implicit ga: Generic.Aux[C, H], selector: Selector[H, R]): R = {
ga.to(value).select[R]
}
And this allows me to choose from the case class:
scala> val s: String = newGetByType(instance)
s: String = host
But this seems to work only for the first type in the case class:
scala> val i: Int = newGetByType(instance)
<console>:17: error: type mismatch;
found : String
required: Int
val i: Int = newGetByType(instance)
Am I on the right track?