www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach/opApply is the visitor pattern

reply BCS <ao pathlink.com> writes:
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)
Jan 30 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Jan 30, 2009 at 5:35 PM, BCS <ao pathlink.com> 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.
Yeah. As far as I know it is.
Jan 30 2009
prev sibling next sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
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)
Indeed, I heard it called that a long time ago, and have explained it as such since then. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango
Jan 31 2009
parent BCS <none anon.com> writes:
Hello Lars,

 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)
 
Indeed, I heard it called that a long time ago, and have explained it as such since then.
Well I guess I just figured out how to cut the time it takes to explain by about 90%. Nifty. Maybe the docs should use the term.
Jan 31 2009
prev sibling next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
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).
Feb 01 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Feb 1, 2009 at 7:10 AM, Robert Fraser
<fraserofthenight 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.
Feb 01 2009
prev sibling parent reply BCS <none anon.com> writes:
Hello Robert,

 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).
OK I'll grant that it doesn't follow the normal pattern to the letter but it can be viewed as a degenerate case where there is only one visitable type.
Feb 01 2009
parent reply grauzone <none example.net> writes:
BCS wrote:
 Hello Robert,
 
 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).
OK I'll grant that it doesn't follow the normal pattern to the letter but it can be viewed as a degenerate case where there is only one visitable type.
Does that mean all function calls are a degenerate case of the visitor pattern?
Feb 01 2009
parent BCS <none anon.com> writes:
Hello grauzone,

 BCS wrote:
 
 Hello Robert,
 
 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).
OK I'll grant that it doesn't follow the normal pattern to the letter but it can be viewed as a degenerate case where there is only one visitable type.
Does that mean all function calls are a degenerate case of the visitor pattern?
no only when you pass a "action" memeber that is called on "stuff".
Feb 01 2009
prev sibling parent reply grauzone <none example.net> writes:
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)
More importantly, why does it matter?
Feb 01 2009
parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
grauzone 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)
More importantly, why does it matter?
Because there is a difference? opApply is more like a functor. Not the visitor pattern in my view of things. They are/can be used to solve different things. The visitor pattern commonly can be used to avoid case object.type: situations, to deal with different object types. Whereas a functor is used for other things, like iteration.
Feb 13 2009