Map on HList in method with Poly1 based on type pa

2019-03-29 09:41发布

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

1条回答
萌系小妹纸
2楼-- · 2019-03-29 10:11

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
查看更多
登录 后发表回答