What is the use of double underscore in repeat() or other steps in Tinkerpop Gremlin?

Viewed 3088

I have noticed double underscore being used in some step functions in Tinkerpop Gremlin 3.3. Could someone please tell why we use this double underscore with an example ? I could not find enough information about this in the documentation.

2 Answers

__. allows you to define an anonymous Traversal, ie. a Traversal which is not bound to a specific TraversalSource.

In the Gremlin console, all Gremlin steps are statically imported so you never need to prefix anonymous traversals with __. unless that anonymous traversal starts with a reserved keyword in the target language. In Groovy, which is the default Gremlin flavor, this is the case with in() and as() steps: because these are reserved keywords, these two steps must be prefixed with __.

In Java, you can avoid __. prefix by statically importing all steps in your program:

import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__

See the small Note section in the documentation: http://tinkerpop.apache.org/docs/3.3.0/reference/#graph-traversal-steps

__ is used to refer to the incoming traversal (vertex or edge) in Java API. For example: in gremlin shell, one can write something like this

graph.traversal().V().repeat(out("edgeType")).until(hasLabel("label")).toList()

But in java one needs an anonymous traversal to call the functions "out" and "hasLabel" within repeat and until (or any other function like by, choose etc.). The above traversal in Java will look like:

graph.traversal().V().repeat(__.out("edgeType")).until(__.hasLabel("label")).toList()
Related