foreach/opApply is the visitor pattern

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Feb 1 09:01:09 PST 2009


On Sun, Feb 1, 2009 at 7:10 AM, Robert Fraser
<fraserofthenight at gmail.com> wrote:
> BCS wrote:
>>
>> Correct me if I'm wrong but I thing that D's opApply is a form of the
>> Visitor pattern where the calling function's stack frame is the visitor
>> object.
>>
>> This just occurred to me. Maybe I've been missing something re the visitor
>> pattern but I think this make for a nice, cool and easy way to describe it.
>> (Also I don't remember it being described that way)
>
> Er.... no. There's no double-dispatch (at least automatically), there's only
> one foreach body delegate. The visitor pattern as far as I know it uses
> dynamic dispatch so the visitor object can handle different objects in the
> class hierarchy differently. For example, you couldn't use a foreach to
> enumerate through a syntax tree and handle expressions and statements
> differently (well, you could, but you'd have to do it manually).

I don't know/think that's a requirement for the actual visitor pattern
to work.  I think it's mostly that iteration is turned inside-out.


More information about the Digitalmars-d-learn mailing list