[英]Shapeless HList return type
I am trying to incorporate a bit of shapeless into my code and am falling at an embarassingly early hurdle.我试图在我的代码中加入一些无形的东西,并且陷入了一个令人尴尬的早期障碍。 In the example below, it seems that HCons-ing an undefined object to an HNil:在下面的示例中,似乎 HCons 将未定义的对象转换为 HNil:
trait HasValue[A, B] {
def get(a: A): B
def getAll[L <: HList, O <: HList](a: A)(implicit ga: GetAll[A, L]): O = ga.getAll(a, HNil)
}
trait GetAll[A, B] {
def getAll[L <: HList, O <: HList](a: A, l: L): O
}
implicit def getAllIfHasValue[A, B](implicit ev: HasValue[A, B]) = new GetAll[A, B] {
def getAll[L <: HList, O <: HList](a: A, l: L): O = ev.get(a) :: l
}
and getting an error - type mismatch: Found B :: L, Required O
.并得到一个错误type mismatch: Found B :: L, Required O
。 I would have thought that, since L
is itself an HList
, B :: L
should itself be an HList
and therefore all should be well.我会认为,因为L
本身就是一个HList
,所以B :: L
本身应该是一个HList
,因此一切都应该很好。 But obviously not.但显然不是。
Any help appreciated!任何帮助表示赞赏!
I guess the error is pretty clear我想错误很明显
type mismatch;
found : B :: L
required: O
ev.get(a) :: l
has type B :: L
but O
is expected. ev.get(a) :: l
有类型B :: L
但O
是预期的。
I would have thought that, since
L
is itself anHList
,B :: L
should itself be anHList
and therefore all should be well.我会认为,因为L
本身就是一个HList
,所以B :: L
本身应该是一个HList
,因此一切都应该很好。
B :: L
is an HList
, indeed. B :: L
确实是一个HList
。 The problem is that B :: L
is not O
.问题是B :: L
不是O
。
And when you write the signature当你写下签名时
def getAll[L <: HList, O <: HList](a: A, l: L): O = ???
this means, for any type L <: HList
and any type O <: HList
, having values a: A
and l: L
produce a value of type O
.这意味着,对于任何类型L <: HList
和任何类型O <: HList
,具有值a: A
和l: L
产生类型O
的值。 I guess that's not what you wanted.我想那不是你想要的。
Maybe you wanted to return a type O
depending on types A
, B
.也许您想根据类型A
、 B
返回类型O
。 Then you can introduce a type parameter然后就可以引入一个类型参数
trait GetAll[A, B] {
type O
def getAll[L <: HList](a: A, l: L): O
}
Or maybe you wanted to return a type O
depending on types A
, B
, L <: HList
.或者,您可能想根据类型A
、 B
、 L <: HList
返回类型O
Then additionally you should transfer L
to the trait level那么另外你应该将L
转移到特质级别
trait GetAll[A, B, L <: HList] {
type O
def getAll(a: A, l: L): O
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.