-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Visitor pattern traversal using double/single dispatch software design pattern #1274
Comments
Hi there. We called it the Because Rascal has a dynamic dispatch mechanism which is not bound by a single receiver object, like in OO, there is no need for something like the "Visitor design pattern" in Rascal. You can dispatch on function parameter, or even two parameters at the same time! As a result, there is no need for "double dispatch" idioms at all in this language. Consider this example:
The add function dispatches to one of the three alternatives (either the constructor or one of the two rewrite rules) based on the value of the left or the right argument (if its Similarly the cases of the Here's an example where
Hope this helps! |
It also helps to realize that Rascal does not have objects. It has modules as encapsulation of a set of functions. The functions do the dispatching and the modules are just collections of these functions. This is very much unlike object-oriented languages where the data and the functions are coupled together semantically. |
About accessing properties:
|
Thank you very much for clarification. I am more than familiar with visitor pattern and I already built control flow generator using Python's Visitor pattern implementation. Every other definition on the internet mentions double dispatch that only makes sense for me when its applied to objects. Edit: You should definitely remove reference to "Visitor pattern Wikipedia" because it will only confuse people since it seems like OO design pattern. This explanation is needed in documentation. Edit2: Consider the following situation: I have an implementation written in object-oriented programming language C++. I decide to use Rascal to parse the C++ into an AST. How do I write visit "module" in such a way that I walk through all the function declarations (in my understanding objects with properties) and print out all their names and arguments. |
The Let's say each formal parameter is represented as
|
Thank you very much for the explanation. It is really appreciated and it clarified what I did not understand. |
Hello again,
Recently I created an issue that Rascal language misses visitor pattern traversal. Your implementation with case and pattern with action by definition is not a Visitor pattern implementation. This software pattern relies on having double dispatch method one on the language nodes and the other one on the visit class itself. Python language handles this with a single dispatch. What Rascal does is that it matches something to a pattern, this is not the same as calling function/method on given language node.
Following image (taken from Wikipedia that you reference in your documentation) showcase simple UML class diagram of visitor pattern implementation.
Where is the visit and accept method? How do you by default visit specific language node? How do you access its properties?
My capacity might be insufficient to understand Rascal's Visitor pattern implementation. If that is the case, could you extend your documentation with an appropriate example to avoid any future confusion?
As a suggeted example here is Python's solution:
The text was updated successfully, but these errors were encountered: