[plt-scheme] Abstracting Visitors (is in Java)

From: Noel Welsh (noelwelsh at yahoo.com)
Date: Thu Jan 4 09:15:47 EST 2007

Interesting question.  If I understand the code correctly
(and that's no sure thing; I found the code very difficult
to read (Java's fault -- excessive casting etc)) there are
two possible implementations:

  !. Pass in the action to perform in the case of the empty

  2. Delegate this decision to the subclass (use an
abstract method)

Do the 1st and you've just about implemented fold.  The 2nd
I consider more OO style, but you end up writing classes
that are really functions, as you need to have a new class
for every possible action

The interplay between OO and FP is certainly interesting!


--- "wooks ." <wookiz at hotmail.com> wrote:

> However there is more that could be abstracted. Both
> forCons methods start 
> with
>       return first instanceof Cons ?
>         new Cons(((Cons)first).accept(this),
> ((AList)rest.accept(this))):
> to deal with the case of nested lists.
> I thought of hoisting that test into an abstract forCons
> class and then 
> calling it via a super method in which case it would
> become
>       if ( first instanceof Cons)
>         return new Cons(((Cons)first).accept(this), 
> ((AList)rest.accept(this)));
> but what would I return if the If statement failed?

Email: noelwelsh <at> yahoo <dot> com   noel <at> untyped <dot> com
AIM: noelhwelsh
Blogs: http://monospaced.blogspot.com/  http://www.untyped.com/untyping/

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

Posted on the users mailing list.