为什么映射Option [T]的HList不起作用?

时间:2014-09-30 18:22:02

标签: scala mapping shapeless hlist

这不编译,我不明白为什么:

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

1 个答案:

答案 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