* [Caml-list] implementing the visitor pattern in ocaml
@ 2003-10-14 9:11 Rafael 'Dido' Sevilla
2003-10-14 10:13 ` Samuel Lacas
0 siblings, 1 reply; 3+ messages in thread
From: Rafael 'Dido' Sevilla @ 2003-10-14 9:11 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] implementing the visitor pattern in ocaml
2003-10-14 9:11 [Caml-list] implementing the visitor pattern in ocaml Rafael 'Dido' Sevilla
@ 2003-10-14 10:13 ` Samuel Lacas
2003-10-14 15:18 ` Didier Remy
0 siblings, 1 reply; 3+ messages in thread
From: Samuel Lacas @ 2003-10-14 10:13 UTC (permalink / raw)
To: caml-list
Hi, you need to use coercions here (at least, that's the way it works
with me). Simply declare
method accept (v : abstractvisitor) = v#visit_foo(self :> foo)
in class foo.
Have a look at section 3.11 of the manual for more information.
Hope this helps,
sL
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] implementing the visitor pattern in ocaml
2003-10-14 10:13 ` Samuel Lacas
@ 2003-10-14 15:18 ` Didier Remy
0 siblings, 0 replies; 3+ messages in thread
From: Didier Remy @ 2003-10-14 15:18 UTC (permalink / raw)
To: Samuel Lacas; +Cc: caml-list
Samuel Lacas <Samuel.Lacas@trusted-logic.fr> writes:
> Have a look at section 3.11 of the manual for more information.
And section 5.3, which also comes with more details at
http://cristal.inria.fr/~remy/cours/appsem/ocaml-objects.html#toc13
Didier
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-10-14 15:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-14 9:11 [Caml-list] implementing the visitor pattern in ocaml Rafael 'Dido' Sevilla
2003-10-14 10:13 ` Samuel Lacas
2003-10-14 15:18 ` Didier Remy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox