Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Forked from milessabin/gist:6448380
Last active December 22, 2015 08:59
Show Gist options
  • Save mpilquist/6449193 to your computer and use it in GitHub Desktop.
Save mpilquist/6449193 to your computer and use it in GitHub Desktop.
import shapeless._
trait F[A] {
def xmap[B](f: A => B, g: B => A): F[B]
def as[B] = new AsAux[B]
class AsAux[B] {
def apply[C](implicit gen: Generic.Aux[B, C], ev: A =:= C): F[B] =
xmap(a => gen.from(ev(a)), b => gen.to(b).asInstanceOf[A])
}
}
case class Bar(x: Int, y: String)
object Example {
val a: F[Int :: String :: HNil] = null
val b: F[Bar] = a.as[Bar].apply // TODO Get rid of need to call apply here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment