Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] implementing the visitor pattern in ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Rafael 'Dido' Sevilla <dido@i...>
Subject: [Caml-list] implementing the visitor pattern in ocaml
I've been experimenting with OCaml's object-oriented features, and have
tried to implement the visitor pattern in OCaml.  Here's one attempt:

class virtual abstractvisitor =
object
  method virtual visit_foo : foo -> unit
end
and foo (x_init : int) =
object (self)
  val x = x_init
  method get_x = x
  method accept (v : abstractvisitor) = v#visit_foo(self)
end
and visitor =
object
  inherit abstractvisitor
  method visit_foo f = print_string ("x = " ^ (string_of_int f#get_x))
end
;;

but I get this:

This expression has type < accept : abstractvisitor -> 'a; get_x : int;
.. >
but is here used with type foo = < .. >
Self type cannot escape its class

What does this mean?  What's the right way to implement the visitor
design pattern in OCaml?

-------------------
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