Scala:如何在函数中返回元组? “类型不匹配”

时间:2019-03-21 13:49:54

标签: scala gremlin-scala

我正在使用库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

不确定如何解决此问题。

1 个答案:

答案 0 :(得分:2)

为什么需要扩展方法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"