Skip to content

Instantly share code, notes, and snippets.

@milessabin
Last active August 29, 2015 14:18
Show Gist options
  • Save milessabin/18a9effd4bb354bd1dd4 to your computer and use it in GitHub Desktop.
Save milessabin/18a9effd4bb354bd1dd4 to your computer and use it in GitHub Desktop.
Demo of SingletonProductArgs from shapeless-2.2.0
scala> :paste
// Entering paste mode (ctrl-D to finish)
import shapeless._, ops.hlist._, ops.record._
object selectAll extends SingletonProductArgs {
class Apply[K <: HList] {
def from[T, R <: HList, S <: HList, Out](t: T)
(implicit
gen: LabelledGeneric.Aux[T, R],
sel: SelectAll.Aux[R, K, S],
tp: Tupler.Aux[S, Out]
): Out =
tp(sel(gen.to(t)))
}
def applyProduct[K <: HList](keys: K) = new Apply[K]
}
case class Quux(i: Int, s: String, b: Boolean, d: Double)
// Exiting paste mode, now interpreting.
import shapeless._
import ops.hlist._
import ops.record._
defined object selectAll
defined class Quux
scala> val quux = Quux(23, "foo", true, 2.0)
quux: Quux = Quux(23,foo,true,2.0)
scala> selectAll('i, 's).from(quux)
res0: (Int, String) = (23,foo)
scala> selectAll('d, 'b, 'i).from(quux)
res1: (Double, Boolean, Int) = (2.0,true,23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment