为什么我不能索引到通过映射到另一个HList获得的HList?

时间:2012-06-26 09:36:31

标签: scala shapeless

即使.head也不起作用。

我需要做出哪些改变才能使其发挥作用?

import shapeless._
import HList._
import Nat._

scala> case class Foo[A](a: A)
defined class Foo

scala> case class Bar[A](f: Foo[A])
defined class Bar

scala> val xs = Foo(23) :: Foo("blah") :: HNil
xs: shapeless.::[Foo[Int],shapeless.::[Foo[java.lang.String],shapeless.HNil]] = Foo(23) :: Foo(blah) :: HNil

scala> object mapper extends (Foo ~> Bar) {
     |   def apply[A](f: Foo[A]) = Bar(f)
     | }
defined module mapper

scala> xs map mapper
res13: mapper.Out = Bar(Foo(23)) :: Bar(Foo(blah)) :: HNil

scala> res13.apply[_1]
<console>:38: error: could not find implicit value for parameter at: shapeless.At[mapper.Out,shapeless.Nat._1]
              res13.apply[_1]
                         ^

scala> res13.head
<console>:38: error: could not find implicit value for parameter c: shapeless.IsHCons[mapper.Out]
              res13.head
                    ^

1 个答案:

答案 0 :(得分:6)

完全按照我的书面编写,至少使用最新的2.10.0-SNAPSHOT,

import shapeless._
import HList._
import Nat._

scala> case class Foo[A](a: A)
defined class Foo

scala> case class Bar[A](f: Foo[A])
defined class Bar

scala> val xs = Foo(23) :: Foo("blah") :: HNil
xs: shapeless.::[Foo[Int],shapeless.::[Foo[String],shapeless.HNil]] = Foo(23) :: Foo(blah) :: HNil                                                                                                     

scala> object mapper extends (Foo ~> Bar) {
     |   def apply[A](f: Foo[A]) = Bar(f)                                                                                                                                                              
     | }                                                                                                                                                                                               
defined module mapper                                                                                                                                                                                  

scala> xs map mapper                                                                                                                                                                                   
res0: shapeless.::[Bar[Int],shapeless.::[Bar[String],shapeless.HNil]] = Bar(Foo(23)) :: Bar(Foo(blah)) :: HNil                                                                                         

scala> res0[_1]
res1: Bar[String] = Bar(Foo(blah))                                                                                                                                                                     

scala> res0.head                                                                                                                                                                                       
res2: Bar[Int] = Bar(Foo(23))

请注意,上面res0推断的类型是Bar[Int] :: Bar[String] :: HNil而不是mapper.Out ...我怀疑这是2.9.x和2.10.0-SNAPSHOT之间的行为差​​异

如果你坚持使用2.9.x那么我认为你应该能够通过明确地将Bar[Int] :: Bar[String] :: HNil归咎于你的res13来解决这个问题...显然这更详细,但是c' est la vie。