首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zipwith产品实现与Shapeless?

Zipwith产品实现与Shapeless?
EN

Stack Overflow用户
提问于 2017-02-22 21:30:31
回答 1查看 59关注 0票数 2

给定:

代码语言:javascript
复制
// Given an HList of size N, provide evidence of the sum of HList
// multiplied by _3 (length) :: _2 (length) :: _1 (length) :: HNil
// Example: input: _1 :: _2 :: _2 -> output: _3 + _4 + _2 :: HNil
trait HListProductZipped[L <: HList] {
  type Out <: HList
}
object HListProductZipped {

  type Aux[L <: HList, Out1 <: HList] = HListProductZipped[L] { type Out = Out1 }

  def apply[L <: HList](implicit ev: HListProductZipped[L]): Aux[L, ev.Out] = ev

  implicit def induct[H <: Nat, T <: HList, L <: Nat](
    implicit ev: Length.Aux[H :: T, L],
             prod: Prod[H, L],
             rest: HListProductZipped[T]
  ): HListProductZipped.Aux[H :: T, prod.Out :: rest.Out] = new HListProductZipped[H :: T] {
    type Out = prod.Out :: rest.Out
  }

  implicit val hnilHListProductZipped: HListProductZipped[HNil] = new 
    HListProductZipped[HNil] {
      type Out = HNil
    }

}

然而,它并没有像我预期的那样工作:

代码语言:javascript
复制
import shapeless._
import nat._
import net.HListProductZipped

scala> val a = HListProductZipped[_1 :: _2 :: HNil]
a: net.HListProductZipped[shapeless.::[shapeless.Succ[shapeless._0],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.HNil]]]{type Out = shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],net.HListProductZipped.hnilHListProductZipped.Out]]} = net.HListProductZipped$$anon$3@130efbff

scala> val e: a.Out = _2 :: _2 :: HNil
<console>:19: error: type mismatch;
 found   : shapeless.::[shapeless.nat._2,shapeless.::[shapeless.nat._2,shapeless.HNil]]
    (which expands to)  shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.HNil]]
 required: a.Out
    (which expands to)  shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],net.HListProductZipped.hnilHListProductZipped.Out]]
       val e: a.Out = _2 :: _2 :: HNil
                         ^

请告诉我我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-22 21:34:35

hnilHListProductZipped的返回类型中缺少优化,这意味着编译器不能知道它的OutHNil。将其更改为Aux[HNil, HNil]将使此工作正常。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42392897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档