Let's say we have the following:
object MyValue {
case class MyBoolean(record: Boolean) extends MyValue
case class MyDouble(record: Double) extends MyValue
}
trait MyValue
and a sealed trait:
object MyValueGrabber {
trait MyBooleanGrabber[T] extends MyValueGrabber[T] {
override def apply(record: T): Option[MyValue.MyBoolean]
}
trait MyDoubleGrabber[T] extends MyValueGrabber[T] {
override def apply(record: T): Option[MyValue.MyDouble]
}
}
sealed trait MyValueGrabber[T] {
def apply(record: T): Option[MyValue]
}
as well as a mapping between the MyValue
type and a different type with which it is associated:
object Mapper {
implicit val myBooleanMapper = new Mapper[MyValueGrabber.MyBooleanGrabber[_], String] {
override def getValue: String = "found a bool"
}
implicit val myDoubleMapper = new Mapper[MyValueGrabber.MyDoubleGrabber[_], Long] {
override def getValue: Long = 100L
}
}
trait Mapper[A, B] {
def getValue: B
}
and then we have:
trait HolyGrail[T] {
def name: String
def myValueGrabber: MyValueGrabber[T]
def getValue[U] = ???
}
The question is how do we use the Mapper
implicitly to return the correct type U
. Ideally, it would be something like:
def getValue[U](implicit ev: Mapper[myValueGrabber.type, U]) = ev.getValue // doesn't work