Why can't I index in an HList obtained by matching with another HList?

.headDoesn't even work.

What changes do I need to make to make this work?

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
                    ^
+5
source share
1 answer

Works for me exactly the same as it is written, at least with the latest 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))

Please note that the type specified for res0above is Bar[Int] :: Bar[String] :: HNilnot mapper.Out... I suspect that this is a difference in behavior between 2.9.x and 2.10.0-SNAPSHOT.

If you are stuck with 2.9.x, then I think you should solve this problem by explicitly attaching Bar[Int] :: Bar[String] :: HNilto your res13... obviously this is more verbose, but c'est la vie.

+6
source

All Articles