【问题标题】:Scala: How to return a tuple in a function? "type mismatch"Scala:如何在函数中返回元组? “类型不匹配”
【发布时间】:2019-08-12 09:32:38
【问题描述】:

我正在使用库 gremlin-scala 与 Janusgraph 进行交互。

使用 DSL,插入新顶点的一种方法是执行以下操作:

val Id = Key[Long]("id")
val Name = Key[String]("name")
graph + ("label", Id -> 42, Name -> "Mike")

我想把这部分做成一个函数("label", Id -> 42, Name -> "Mike")

case class VertexModel(id: Long, name: String) {
  def toVertex: (Label, KeyValue[Long], KeyValue[String]) = {
    val Id = Key[Long]("id")
    val Name = Key[String]("name")
    ("item", Id -> id, Name -> name)
  }
}

val model = VertexModel(1, "Bill")
graph + model.toVertex

这会失败并出现以下错误:

Error:(26, 11) type mismatch;
 found   : T1
 required: gremlin.scala.Label
    (which expands to)  String
    graph + vertex
Error:(26, 11) type mismatch;
 found   : T2
 required: gremlin.scala.KeyValue[Long]
    graph + vertex
Error:(26, 11) type mismatch;
 found   : T3
 required: gremlin.scala.KeyValue[String]
    graph + vertex

不知道如何解决这个问题。

【问题讨论】:

  • gremlin-scala 的源代码看来,图表上+ 的参数不是元组,而只是一个常规参数列表。

标签: scala gremlin-scala


【解决方案1】:

为什么需要扩展方法toVertex

这不是像

import gremlin.scala._
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph

object App {

  implicit val graph: ScalaGraph = TinkerGraph.open.asScala

  case class VertexModel(id: Long, name: String) 

  val model = VertexModel(1, "Bill")
  graph + model
}

?

build.sbt

scalaVersion := "2.12.8"
libraryDependencies += "com.michaelpollmeier" %% "gremlin-scala" % "3.4.0.4"
libraryDependencies += "org.apache.tinkerpop" % "tinkergraph-gremlin" % "3.4.0"

【讨论】:

  • 太酷了!不知道这是可能的。还有 1 个问题:如何将标签添加到课程中?扩展您的解决方案:case class VertexModel(label: Label, id: Long, name: String),我得到Name cannot be in protected namespace: label
  • 抱歉,无法重现。 case class VertexModel(label: Label, id: Long, name: String) val model = VertexModel("item", 1, "Bill") graph + model 为我编译。
  • gremlin 服务器在运行时
  • 另一个问题是我不能使用不同的Key 名称。例如我想要一个键 type 但我不能做 case class VertexModel(type: String, id: Long, name: String) 因为 type 是一个关键字。
  • 如果您在反引号中尝试label, type 会怎样?
猜你喜欢
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2015-09-23
相关资源
最近更新 更多