Code generator and visitor pattern

Mick Krippendorf mad.mick at gmx.de
Sun Jul 18 04:09:17 EDT 2010


Mark Lawrence wrote:
> On 17/07/2010 20:38, Mick Krippendorf wrote:
>>
>> If Java were *really* a multiple dispatch language, it wouldn't be
>> necessary to repeat the accept-code for every subclass. Instead a single
>> accept method in the base class would suffice. In fact, with true
>> multiple dispatch VP wouldn't even be needed.
> 
> Boilerplate, boilerplate everywhere, but not a beer to drink.

That's Java for ya.

<python>

class ASTNode:
    def accept(self, visitor):
	getattr(visitor, self.__class__.__name__)(self)

class IfNode(ASTNode):
    ... # inherits generic accept method

class ElseNode(ASTNode):
    ... # inherits generic accept method

class PrettyPrinter:
    def IfNode(self, node):
        ...
    def ElseNode(self, node):
        ...

</python>

Regards,
Mick.



More information about the Python-list mailing list