这不编译,我不明白为什么:
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
这有点令人惊讶,因为这会编译:
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
答案 0 :(得分:6)
问题是您使用的是Some
而不是Option
。编译器抱怨说,虽然正在使用Some
映射器,但它无法找到任何转换Option
列表的内容。要解决此问题,您可以:
将简单列表构造更改为使用Option
而不是Some
:
object option extends (Option ~> List) {
def apply[T](t: Option[T]) = t.toList
}
val simple = Option(1) :: Option("hello") :: Option(true) :: HNil
val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option
使用Some
作为映射器的类型:
object option extends (Some ~> List) {
def apply[T](t: Some[T]) = t.toList
}
val simple = Some(1) :: Some("hello") :: Some(true) :: HNil
val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option
或强制将简单类型转发为Option
:
object option extends (Option ~> List) {
def apply[T](t: Option[T]) = t.toList
}
val simple: Option[Int] :: Option[String] :: Option[Boolean] :: HNil = Some(1) :: Some("hello") :: Some(true) :: HNil
val opts2: List[Int] :: List[String] :: List[Boolean] :: HNil = simple map option