繁体   English   中英

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

[英]Scala: How to return a tuple in a function? "type mismatch"

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

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

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

?

生成.sbt

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM