Created
May 29, 2014 22:49
-
-
Save jroesch/52727c6d77a9d98458d5 to your computer and use it in GitHub Desktop.
List => Product conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package object As { | |
| import shapeless._ | |
| import syntax.typeable._ | |
| import syntax.std.traversable._ | |
| import ops.traversable._ | |
| implicit def asOps[A](xs: List[A]) = new AsOps(xs) | |
| final class AsOps[A](xs: List[A]) { | |
| def as[U](implicit as: As[U]): Option[U] = as.apply(xs) | |
| } | |
| trait As[U] { | |
| type Repr | |
| def apply(xs: List[Any]): Option[U] | |
| } | |
| object As { | |
| /* not sure about this */ | |
| //def apply[P <: Product, L <: List[_]](implicit as: As[P]): As.Aux[P, as.Repr] = as | |
| type Aux[P <: Product, L <: HList] = As[P] { type Repr = L } | |
| implicit def provideAs[P <: Product, L <: HList](implicit gen : Generic.Aux[P, L], ty : Typeable[L], fl: FromTraversable[L]): As.Aux[P, L] = new As[P] { | |
| type Repr = L | |
| def apply(xs : List[Any]): Option[P] = xs.toHList[L] map (gen.from(_)) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment