Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Visitor pattern (was copy of parametrized object)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Bow-Yaw Wang <bywang@s...>
Subject: Re: [Caml-list] Visitor pattern (was copy of parametrized object)

Thanks a lot. Using polymorphic variants solves the problem
beautifully. I'll reorganize my code. Thanks!

Bow-Yaw

> By the way, I'm not sure this coding of the visitor pattern is a good
> programming approach in ocaml: most things can be done in a simpler
> way using a function rather than a visitor, since anyway generally all
> cases of a visitor have the same return type.
> 
> # class a = 
>   object 
>     method invite : 'cases 'value. (([> `A] as 'cases) -> 'value) -> 'value =
>       fun f ->  f `A
>   end;;
> class a : object method invite : ([> `A ] -> 'a) -> 'a end
> 
> The advantage is two-fold: you don't need to define a class for each
> visitor (this is a pain), and now the variables 'cases and 'value are
> really independent, so if you want to define the class a in a more
> extensible way, you can write:
> 
> # class ['cases] a = 
>   object 
>     method invite : 'value. (([> `A] as 'cases) -> 'value) -> 'value =
>       fun f ->  f `A
>   end;;
> class ['a] a :
>   object constraint 'a = [> `A ] method invite : ('a -> 'b) -> 'b end
> # let x = new a;;
> val x : _[> `A ] a = <obj>
> # x#invite (fun `A -> 0);;
> - : int = 0
> # x#invite (fun `A -> "a");;
> - : string = "a"
> 
> Another remark is that in most cases there is no need to wrap your
> variant type in an object anyway, and then you don't need classes at
> all :-)
> 
> Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners