Shapeless和gremlin scala:如何将调用结果返回给`as`? [英] Shapeless and gremlin scala: How do I return the result of a call to `as`?

查看:90
本文介绍了Shapeless和gremlin scala:如何将调用结果返回给`as`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在调用此函数as(来自gremlin-scala):

So, I am calling this function as (from gremlin-scala):

case class GremlinScala[End, Labels <: HList](traversal: GraphTraversal[_, End]) {
  def as(name: String, moreNames: String*)(implicit p: Prepend[Labels, End :: HNil]) =
    GremlinScala[End, p.Out](traversal.as(name, moreNames: _*))
}

它在这里定义:它带有一个隐式的Prepend参数,我不确定我是否理解.我知道gremlin-scala使用其HList来跟踪查询as中的哪些点被调用,以便稍后在select被调用时它知道要返回的遍历中的哪些点.

It takes an implicit Prepend argument, which I am not sure I understand. I know that gremlin-scala uses its HList to keep track of which points in the query as is called, so that later when select is called it knows which points in the traversal to return.

这是关键:as追加到该HList.或视情况而定.

This is the key: as appends to that HList. Or prepends apparently, as the case may be.

这在常规代码中可以正常工作,但是现在我想编写一个调用as并返回其结果的函数.这就是我遇到的问题:此函数的返回值的签名是什么?

This works fine in general code, but now I want to write a function that calls as and returns its result. This is where I am stuck: what is the signature of the return value of this function?

最后,我在函数中添加了一个隐式参数,但是我担心我只是将问题逐个上了一层.这是我到目前为止的内容:

Finally, I have added an implicit param to my function, but I fear I have just chased the problem up a level. Here is what I have so far:

case class AsOperation[A, In <: HList](step: String) extends Operation {
  def operate(g: GremlinScala[A, In]) (implicit p: Prepend[In, ::[A, HNil]]): GremlinScala[A, p.Out] = {
    g.as(step)
  }
}

这使它可以编译,但是我仍然不能使用此功能!每当我叫它时,它都会向我抱怨

This makes it compile, but I still can't use this function! Whenever I call it, it complains to me that

could not find implicit value for parameter p: shapeless.ops.hlist.Prepend[In,shapeless.::[A,shapeless.HNil]]

我该如何编写一个返回as结果的函数,它的签名是什么?

How do I write a function that returns the result of as, and what is its signature?

谢谢!

推荐答案

正如您正确解释的那样,我们使用prepend的原因是保留标有as的步骤的类型.之所以将它们保持相反的顺序,是因为它们在两侧的处理都更加容易:用于捕获和重放.

As you explain correctly, the reason we use prepend is to keep the types of the steps labelled with as. It's keeping them in the reverse order since it's easier to process on both sides: for capturing and replaying.

因此,implicit p: Prepend[Labels, End :: HNil]在当前步骤的前面加上类型,以便我们在第二个type参数中捕获它(并可以在以后的步骤中使用它,例如select).

So the implicit p: Prepend[Labels, End :: HNil] is prepending the type at the current step so that we have it captured in the second type parameter (and can use it in later steps, e.g. select).

据我所知,您正在做的事情完全正确,而且实际上对我来说是有效的...)

As far as I can see you're doing exactly the right thing, and it actually works... for me anyway :)

它将编译:

import gremlin.scala._
import shapeless.{HNil, ::}
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory
def graph = TinkerFactory.createModern.asScala
val gs1: GremlinScala[Vertex, Vertex :: HNil] = graph.V().as("a")
val gs2: GremlinScala[Vertex, Vertex :: HNil] = AsOperation("someLabel").operate(graph.V())

这篇关于Shapeless和gremlin scala:如何将调用结果返回给`as`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆