Skip to content

Instantly share code, notes, and snippets.

@frosforever
Created December 1, 2017 21:03
Show Gist options
  • Save frosforever/21ff5881690c324382916cba2af80c38 to your computer and use it in GitHub Desktop.
Save frosforever/21ff5881690c324382916cba2af80c38 to your computer and use it in GitHub Desktop.
import shapeless._
import shapeless.ops.hlist
object mapper extends Poly1 {
implicit def caseOpt[T] = at[Option[T]](_ => Option.empty[T])
}
def foo[S, SRep <: HList, O <: HList](s: S)(implicit
labelledGeneric: Generic.Aux[S, SRep],
map: hlist.Mapper.Aux[mapper.type, SRep, O],
i1: Generic.Aux[S, O]
) = {
i1.from(
labelledGeneric.to(s).map(mapper)
)
}
case class Qai(a: Option[String], b: Option[Int])
println {
foo(Qai(Some("asdf"), Some(1))) //Qai(None, None)
}
case class Bar(a: Int, b: Option[Int])
// foo(Bar(1, None)) //Will Not compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment