This does not compile and I do not understand why:
import shapeless._
import poly._
object option extends (Option ~> List) {
def apply[T](t: Option[T]) = t.toList
}
val simple = Some(1) :: Some("hello") :: Some(true) :: HNil
val opts: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option
This is somewhat surprising, because this does compile:
import shapeless._
import poly._
object choose extends (Set ~> Option) {
def apply[T](s: Set[T]) = s.headOption
}
val sets = Set(1) :: Set("foo") :: HNil
val opts: Option[Int] :: Option[String] :: HNil = sets map choose
The problem is your usage of
Some
instead ofOption
. The compiler complains that it can't find anything to convert a list ofSome
s though yourOption
mapper is being used. To fix this you can:Change your simple list construction to use
Option
instead ofSome
:Use
Some
as the type of your mapper:Or force the type of simple to be downcast to
Option
: