Is there a way to map a natural transformation (e.g. a Option ~> Either[String, *]) over a KList (e.g. a HList with a UnaryTCConstraint)? That would seem to be the natural thing to do with a KList.
Specifically I was trying to do the following:
object myNaturalTransformation extends (Option ~> Either[String, *]) {
def apply[T](a: Option[T]): Either[String, T] = a.toRight("oh noe!")
}
def doStuff[KList <: HList: *->*[Option]#λ](klist: KList) = {
klist.map(myNaturalTransformation)
}
I understand that the missing piece is the Mapper required to perform the .map and that Shapeless isn't able to generate one from myNaturalTransformations cases and the UnaryTCConstraint. Is it possible to obtain one some other way? Or is there another approach to map over a KList that I'm overlooking (apart from passing a Mapper to the doStuff-function)?
I was able to write my own version of UnaryTCConstraint that includes a
def mapper[G[_], HF <: ~>[TC, G]](hf: HF): Mapper[hf.type, L]
to explicitly generate a mapper for a given natural transformation. However I am curious if it's possible to do that with Shapeless' implementation of UnaryTCConstraint.
UnaryTCConstraint(*->*) is not for mapping, it's a constraint (common forHLists,Coproducts, case classes and sealed traits). For mapping there are type classesNatTRel,Mapped,Comapped,Mapperetc. (separate forHLists andCoproducts).Try both constraint and type class
or just type class
or hiding type parameter
LviaPartiallyAppliedpatternor hiding type parameter
Lvia existential (but then return type is not precise)or using extension method
Testing: