* [Caml-list] Recursive type and class
@ 2002-08-30 12:22 David Frese
2002-08-31 3:25 ` John Max Skaller
0 siblings, 1 reply; 2+ messages in thread
From: David Frese @ 2002-08-30 12:22 UTC (permalink / raw)
To: Caml-list
Hello folks,
I have a problem with mixing a class and a type recursively. Written
down directly, I want to have something like this:
type atype = Obj of aclass | I of int
class aclass = object
val mutable field0 : atype = I 5
end
But this does not work, of course. Can anyone tell me, how this can be
done?
Thanks, David.
-------------------
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] 2+ messages in thread
* Re: [Caml-list] Recursive type and class
2002-08-30 12:22 [Caml-list] Recursive type and class David Frese
@ 2002-08-31 3:25 ` John Max Skaller
0 siblings, 0 replies; 2+ messages in thread
From: John Max Skaller @ 2002-08-31 3:25 UTC (permalink / raw)
To: David Frese; +Cc: Caml-list
David Frese wrote:
> Hello folks,
>
> I have a problem with mixing a class and a type recursively. Written
> down directly, I want to have something like this:
>
> type atype = Obj of aclass | I of int
>
> class aclass = object
> val mutable field0 : atype = I 5
> end
>
> But this does not work, of course. Can anyone tell me, how this can be
> done?
You first declare a class *type* with a type parameter:
class type ['t] aclass_t' = object val mutable field0: 't end
Now you declare the recursion:
type atype = Obj of atype aclass_t' | I of int
and finally you make a typedef for the class:
type aclass_t = atype aclass
Well that's got the typing right, so now you can
provide an implementation:
class ['t] aclass init = object val mutable field0: atype = init end
Note that the init value had to be lifted out, so you need a monomorphic
function to instantiate the instance class:
let mkclass () = new aclass (I 5)
Finally, to check it works:
# Obj (mkclass ());;
- : atype = Obj <obj>
[You might be able to make this simpler using a constraint,
I've never quite figured them out :]
--
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850
-------------------
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] 2+ messages in thread
end of thread, other threads:[~2002-08-31 3:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-30 12:22 [Caml-list] Recursive type and class David Frese
2002-08-31 3:25 ` John Max Skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox