基于类型参数的Poly1映射方法 [英] Map on HList in method with Poly1 based on type parameter of class
本文介绍了基于类型参数的Poly1映射方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类,使用 HList
和其他类型进行参数化。如何在> HList
中的某个方法中使用 map
?
编译此代码会抛出 java.lang.AssertionError
:
class Test [L 类型Cont [T] =(P,T)
对象生成器扩展(Id-> Cont){
def apply [T](t:T)= p - > t
def test(隐式m:映射器[generator.type,L])= {
l映射生成器
}
}
new Test(1 :: HNil,'a).test // java.lang.AssertionError
我的目标是这样的结果:
类型Cont [T] =(Symbol,T)
val p = '
对象生成器扩展(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
解决方案
这是Scala编译器(2.9.2和2.10.0-RC1)中的一个错误。
解决方法是,如果将创建的 Test
的实例以及跨两个表达式调用 test
方法,然后按预期工作,
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
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
解决方案
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
这篇关于基于类型参数的Poly1映射方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文