API changes: extend visitor with return values
See original GitHub issueIf you try to build a CopyVisitor of some kind of object hierarchy, one has to build some kind of Stack structure, to give the actual visitor a hint, where it should write its copied data.
I suggest to extend all visitor methods with a return value:
MyType visitor(MyType param);
The standard implementation should return the actual object inserted.
public interface StatementVisitor {
Comment visit(Comment comment);
Commit visit(Commit commit);
Delete visit(Delete delete);
Update visit(Update update);
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:19 (17 by maintainers)
Top Results From Across the Web
java - Return a value from Visitor - Stack Overflow
Suppose we have following classes which we can't change: interface Base { ...
Read more >Visitor Design Pattern in C# - Code Maze
The Visitor pattern allows decoupling algorithms from the objects on which they operate. It allows adding and changing functionalities in a ...
Read more >Interface AnnotationValueVisitor<R,P> - Oracle Help Center
Classes implementing this interface are used to operate on a value when the type of that value is unknown at compile time. When...
Read more >Visitor Design Pattern in Java - DigitalOcean
Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of...
Read more >Extending the REST API (REST Application Developer's Guide)
Your extension can return JavaScript objects and XML, JSON, text, or binary document nodes. A JavaScript object is serialized as JSON before returning...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with @sivaraam but would go one step further and make the extra visitor generic:
This would:
implements GenericStatementVisitor<Statement>, GenericExpressionVisitor<Expression>
, etc.T
set toList<TableName>
(this has the added benefit of easily making the class thread-safe because you no longer have a field lying around).To add to what @SerialVelocity suggested:
I really like the Presto parser API. For example, I have some code where I am converting from SQL to a datalog dialect, and this is one example of how I use the Presto API to do so.
And elsewhere, I can do this:
Note that the visit methods can receive a context and a return value, both of which are generic types parameterized by AstVisitor<X, Y>. And in the above case, the ParseLiterals class is purely stateless (so I can re-use a single instance for the lifetime of my program).
I’ve been trying out the JSQLParser API simply because Presto is an odd SQL dialect and doesn’t support some statements I need. But writing something like the above becomes cumbersome. The return values and the context I pass through the visitors (generic parameters X and Y) now become fields.
Here’s the AstVisitor implementation in Presto: https://github.com/prestodb/presto/blob/master/presto-parser/src/main/java/com/facebook/presto/sql/tree/AstVisitor.java