Skip to content

Instantly share code, notes, and snippets.

@jroesch
Created May 29, 2014 22:49
Show Gist options
  • Select an option

  • Save jroesch/52727c6d77a9d98458d5 to your computer and use it in GitHub Desktop.

Select an option

Save jroesch/52727c6d77a9d98458d5 to your computer and use it in GitHub Desktop.
List => Product conversion
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