Visitor pattern: Difference between revisions

Content added Content deleted
(Created Nim solution.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
Line 467: Line 467:
===Translation of C# example===
===Translation of C# example===
As is often the case in practice, the following example departs somewhat from the typical operation of the pattern described above. There is no abstract Visitor class - only a concrete Visitor class - and the 'visit' methods are called something else.
As is often the case in practice, the following example departs somewhat from the typical operation of the pattern described above. There is no abstract Visitor class - only a concrete Visitor class - and the 'visit' methods are called something else.
<syntaxhighlight lang="ecmascript">class ExpressionPrintingVisitor {
<syntaxhighlight lang="wren">class ExpressionPrintingVisitor {
construct new(){}
construct new(){}


Line 540: Line 540:
{{libheader|Wren-str}}
{{libheader|Wren-str}}
Note that Wren is dynamically typed and can only overload methods based on arity and not on argument type. In the following example, rather than having separate methods for each element type, we instead have a single 'visit' method which tests the type of the argument at run time and takes the appropriate action.
Note that Wren is dynamically typed and can only overload methods based on arity and not on argument type. In the following example, rather than having separate methods for each element type, we instead have a single 'visit' method which tests the type of the argument at run time and takes the appropriate action.
<syntaxhighlight lang="ecmascript">import "./str" for Str
<syntaxhighlight lang="wren">import "./str" for Str


// abstract class
// abstract class