visitor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

visitor

Oleg Korsak
Hi! Why there are "super visit*: anObject" in some visit* methods of PRWikiWriter? For example #visitHeader and #visitListItem. But some methods doesn't use "super visit*: anObject". Where is a difference of this need to use "super visit*: anObject" or not?

Thanks



_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: visitor

Lukas Renggli-2
> Hi! Why there are "super visit*: anObject" in some visit* methods  
> of PRWikiWriter? For example #visitHeader and #visitListItem. But  
> some methods doesn't use "super visit*: anObject". Where is a  
> difference of this need to use "super visit*: anObject" or not?

It is probably the easiest if you put a #halt somewhere and stop  
trough the code, then you see what actually happens.

To explain it in words: you are walking over the wiki document AST.  
It has leaf (PRText, PRAnchor, PRHorizontalRule, ...) and non-leaf  
nodes (PRParagraph, PRTableCell, PRFormat, ...). All the non-leaf  
nodes are subclasses of PRDocumentGroup that defines the logic for  
child-elements.

Now in the visitor, if you have for example a look at

     PRWikiWriter>>visitHorizontalRule: anObject
         self newLine; nextPutAll: anObject class markup

With PRParagraph this looks different:

     PRWikiWriter>>visitParagraph: anObject
         self newLine.
         super visitParagraph: anObject

If you have a look at the super implementation you see it delegates  
(along its hierarchy) to

     PRVisitor>>visitParagraph: anObject
         self visitDocumentGroup: anObject

and finally to

     PRVisitor>>visitDocumentGroup: anObject
         self visitAll: anObject children

So it is used to get the default behavior of the superclass, what is  
"visit my children" in this case.

All visitors basically delegate along their hierarchy to get default  
behavior. In rare cases I use shortcuts (for example the search), to  
avoid walking trough the whole model.

So, not implementing means default behavior. Calling super means  
added behavior + default behavior.

Does that make sense?

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch



_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki