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

て烟熏妆下的殇ゞ 提交于 2019-12-05 01:38:11

问题


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:


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


来源:https://stackoverflow.com/questions/13098296/map-on-hlist-in-method-with-poly1-based-on-type-parameter-of-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!