Search code examples
scalagremlin-scala

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


I'm using the library gremlin-scala to interact with Janusgraph.

Using the DSL, a way to insert a new vertex is by doing the following:

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

I want to make this part into a a function ("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

This fails with the following error:

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

Not sure how to fix this.


Solution

  • Why do you need extension method toVertex?

    Doesn't this work just like

    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"