Visitor pattern revisited in D

Michal Minich michal.minich at gmail.com
Mon Aug 27 09:02:53 PDT 2012


On Monday, 27 August 2012 at 15:00:11 UTC, deadalnix wrote:
> /!\ Shameless autopromotion incoming /!\
>
> I have recently put some effort into exploring alternatives to 
> visitor pattern and see what can be done in D. I ended up with 
> a solution which is a real improvement compared to plein old 
> visitor pattern and wanted to share this here.
>
> I think this is short enough to be a good example to show what 
> can be done with D capabilities.
>
> http://www.deadalnix.me/2012/08/25/visitor-pattern-revisited-in-d/

This is nice use of d magic. If the implementation could 
recompute the functions pointers during compilation, this seems 
as nice generally function. This makes me thing of computed goto 
(to functions)

One possibility to achieve this result for visitor is to use 
mixin.

I would mixin implementation of accept method into very node 
super class. This is to avoid code duplication.

Secondly, I would mixin implementation of opDispatch that simply 
does nothing/throws to every implementation of visitor. This is 
to avoid empty implementations of visit methods which are not 
needed for particular visitor.


More information about the Digitalmars-d-announce mailing list