caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Max Skaller <skaller@ozemail.com.au>
To: David Frese <dfrese@dfrese.de>
Cc: Caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Recursive type and class
Date: Sat, 31 Aug 2002 13:25:43 +1000	[thread overview]
Message-ID: <3D703737.5030508@ozemail.com.au> (raw)
In-Reply-To: <1030710136.1831.11.camel@pc-6>

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


      reply	other threads:[~2002-08-31  3:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-30 12:22 David Frese
2002-08-31  3:25 ` John Max Skaller [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3D703737.5030508@ozemail.com.au \
    --to=skaller@ozemail.com.au \
    --cc=caml-list@inria.fr \
    --cc=dfrese@dfrese.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).