Here is my (not perfect) solution:
@ object Tuple2List {
import shapeless._
import syntax.std.tuple._
import ops.hlist.ToTraversable
trait Imp[A] {
def apply[P <: Product, L <: HList](p: P)
(implicit gen: Generic.Aux[P, L],
lub: LUBConstraint[L, Option[A]],
trav: ToTraversable.Aux[L, List, Option[A]]): List[A] =
gen.to(p).toList.flatMap(_.toList)
}
def apply[A] = new Imp[A]{ }
}
defined object Tuple2List
@ val xs = (Option(1), None, Option(2))
xs: (Option[Int], None.type, Option[Int]) = (Some(1), None, Some(2))
@ Tuple2List[Int](xs)
res9: List[Int] = List(1, 2)
@ val ys = (Option(1), None, Option(2), None, Option(3))
ys: (Option[Int], None.type, Option[Int], None.type, Option[Int]) = (Some(1), None, Some(2), None, Some(3))
@ Tuple2List[Int](ys)
res11: List[Int] = List(1, 2, 3)
Note that you need the pass type parameter Ato make the scala compiler happy.
source
share