首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >探索shapeless的`Last`类型类

探索shapeless的`Last`类型类
EN

Stack Overflow用户
提问于 2017-01-16 06:03:43
回答 1查看 84关注 0票数 1

我或多或少地输入了shapelessLastLast类型类

代码语言:javascript
复制
import shapeless.{HList, HNil, ::}

trait Last[H <: HList] {
  type Out
  def last(in: H): Out
}

然后,据我所知,我键入了HListLast类实例

代码语言:javascript
复制
object Last {
  type Aux[L <: HList, O] = Last[L] { type Out = O }

  // arrived at the truly `last` item, i.e. `H`
  implicit def singleLast[H]: Aux[H :: HNil, H] = new Last[H :: HNil] {
    override type Out = H
    override def last(in: H :: HNil): H = in.head
  }

  // I believe this is the inductive step
  implicit def hlistLast[H, T <: HList, OutT]
  (implicit lt : Last.Aux[T, OutT]): Aux[H :: T, OutT] =
    new Last[H :: T] {
      type Out = OutT
      def apply(l : H :: T): Out = lt(l.tail)
    }
}

然而,我不明白为什么它不能编译:

代码语言:javascript
复制
[error] /Users/kevinmeredith/Workspace/shapeless-sandbox/src/
      main/scala/net/ops.scala:17: net.Last.Aux[T,OutT] does not take parameters
[error]       def apply(l : H :: T): Out = lt(l.tail)
[error]                                      ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

我如何修复这个编译时错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-16 11:47:58

Last的实际非成形实现如下所示:

代码语言:javascript
复制
trait Last[H <: HList] {
  type Out
  def apply(in: H): Out
}

您将apply更改为last,但在hlistLast中您仍在尝试使用apply (通过定义它并在lt上使用它):

代码语言:javascript
复制
def apply(l : H :: T): Out = lt(l.tail)

编译器错误来自于试图在lt.apply不存在的情况下使用它。在这种情况下,如果编译器首先告诉您last未实现,会更有帮助。

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

https://stackoverflow.com/questions/41666832

复制
相关文章

相似问题

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