Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2003-10-14 (09:11)
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 =
  method virtual visit_foo : foo -> unit
and foo (x_init : int) =
object (self)
  val x = x_init
  method get_x = x
  method accept (v : abstractvisitor) = v#visit_foo(self)
and visitor =
  inherit abstractvisitor
  method visit_foo f = print_string ("x = " ^ (string_of_int f#get_x))

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 Archives:
Bug reports: FAQ:
Beginner's list: