Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pure visitor patterns
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: brogoff <brogoff@s...>
Subject: Re: [Caml-list] Pure visitor patterns
On Thu, 28 Dec 2006, Jacques Garrigue wrote:
> From: Jason Hickey <jyh@cs.caltech.edu>
> > I've been trying to write pure visitors (visitors that compute without
> > side-effects).

I'm curious, what's the application? I asked a similar question to yours
a while ago and Jacques (I believe?) suggested that I would be better
off using an imperative approach in OCaml so all my visitors would have
foo -> unit types. I was disappointed at the time but I think it was a
very good suggestion. My visitors are rather complicated and I found it
useful to have open_foo/close_foo (before_visit/after_visit) methods
with different types than the visits. I decided that using side effects
is better than getting too complex with types.

> >     - Is there some other style that would solve this problem?
>
> Not really. Using private rows and recursive allow for some more
> expressiveness (in particular you can then define pure visitors on
> extensible on an extensible collection of classes), but they are a bit
> tricky to use in this context, so I'm not sure this is an improvement
> for simple cases.

I guess you mean recursive modules above. My usual issue with rows is
that they force you to write a lot of stuff out by hand when you wish
there was a way to assemble them from pieces, if you get my meaning.
A petty complaint, to be sure, but there you have it.

BTW, I assume that the virtual instance variables in the next OCaml are
for extensible visitors, right?

> Another trick to make this pattern more scalable is to use constraints
> for parameters.

That's a nice trick! I knew every little piece of it from reading the
docs and knowing how to break some recursions, but I never put it all
together. Thanks. It would be great if you could flesh out a few of
these non-obvious tricks and put them in the OCaml manual.

> class type ['a, 'cases] visitor =
>   object ('self)
>     constraint 'cases = <foo: 'foo; bar: 'bar; ..>
>     method visit_foo : 'foo -> 'a
>     method visit_bar : 'bar -> 'a
>   end
> class type foo =
>   object ('self)
>     method accept : 'a. ('a, cases) visitor -> 'a
>     method examine : int
>   end
> and bar =
>   object ('self)
>     method accept : 'a. ('a, cases) visitor -> 'a
>     method examine : bool
>   end
> and cases = object method foo : foo method bar : bar end

-- Brian