Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map on HList in method with Poly1 based on type parameter of class

I have class, parameterized with HList and some other type. How can I use map on HList in one of its methods?

Compilation of this code throws java.lang.AssertionError:

class Test[L <: HList, P](l: L, p: P) {
  type Cont[T] = (P, T)
  object generator extends (Id ~> Cont) {
    def apply[T](t: T) = p -> t
  }
  def test(implicit m: Mapper[generator.type, L]) = {
    l map generator
  }
}

new Test(1 :: HNil, 'a).test // java.lang.AssertionError

My goal is this kind of result:

type Cont[T] = (Symbol, T)
val p = 'a
object generator extends (Id ~> Cont) {
  def apply[T](t: T) = p -> t
}

scala> (1 :: 'b' :: HNil) map generator
res0: shapeless.::[(Symbol, Int),shapeless.::[(Symbol, Char),shapeless.HNil]] = ('a,1) :: ('a,b) :: HNil
like image 769
senia Avatar asked Oct 27 '12 07:10

senia


1 Answers

This is a bug in the Scala compiler (both 2.9.2 and 2.10.0-RC1).

As a workaround, if you split the creation of the instance of Test and the invocation of the test method across two expression then it works as expected,

scala> val t = new Test(1 :: HNil, 'a)
t: Test[shapeless.::[Int,shapeless.HNil],Symbol] = Test@4b153b34

scala> t.test
res0: shapeless.::[(Symbol, Int),shapeless.HNil] = ('a,1) :: HNil
like image 72
Miles Sabin Avatar answered Oct 12 '22 11:10

Miles Sabin