简体   繁体   English

无形的 HList 返回类型

[英]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 :: LO是预期的。

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 ,因此一切都应该很好。

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: Al: L产生类型O的值。 I guess that's not what you wanted.我想那不是你想要的。

Maybe you wanted to return a type O depending on types A , B .也许您想根据类型AB返回类型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 .或者,您可能想根据类型ABL <: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM