caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: ohl@physik.uni-wuerzburg.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Creating mutually dependent objects?
Date: Sun, 04 Aug 2002 08:28:12 +0900	[thread overview]
Message-ID: <20020804082812N.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <15690.48916.704957.774489@wptx47.physik.uni-wuerzburg.de>

From: Thorsten Ohl <ohl@physik.uni-wuerzburg.de>

> I need to create tuples of mutually dependent objects.  A working
> implementation adds a mutable instance variable that is updated after
> all objects in a tuple have been created.  With a little bit of work,
> this variable can then be hidden.  However, this is a bit tedious.
> Why is the much more elegant approach using `let rec' forbidden?
> 
> Is there a chance that this restriction will be lifted or would such
> permissiveness undermine type safety?

Sure it would: the type system cannot ensure that o1 and o2 will be
initialized at the right time. That is, you could end up with null
pointer errors, which from ocaml point of view are holes in the type
safety. By not allowing them, ocaml also avoids the cost of
dynamically checking for null pointer, as Java does for instance.

A generic answer to that is to use lazy variables (they are more
clever in 3.05).

# class o n o' =
  object (_ : 'a)
    method o' : 'a =     Lazy.force o'
    method n : int = n
  end;;    
class o : int -> 'a Lazy.t -> object ('a) method n : int method o' : 'a end
# let rec o1 = lazy (new o 1 o2) and o2 = lazy (new o 2 o1);;
val o1 : o Lazy.t = <lazy>
val o2 : o Lazy.t = <lazy>
# let o1 = Lazy.force o1 and o2 = Lazy.force o2;;
val o1 : o = <obj>
val o2 : o = <obj>

Note that it will not protect you against null pointer errors, it will
just allow you to detect them at runtime. You can see this with:

# class o n o' =
  let o' = Lazy.force o' in
  object (_ : 'a)   
    method o' : 'a = o'
    method n : int = n
  end;;    
class o : int -> 'a Lazy.t -> object ('a) method n : int method o' : 'a end
[..]
# let o1 = Lazy.force o1 and o2 = Lazy.force o2;;
Exception: Lazy.Undefined.
(Stack overflow with previous versions)

Jacques Garrigue
-------------------
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-03 23:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-02 17:19 Thorsten Ohl
2002-08-03 23:28 ` Jacques Garrigue [this message]
2002-08-04  0:53 ` John Prevost
2002-08-04  2:08   ` Thorsten Ohl

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=20020804082812N.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=ohl@physik.uni-wuerzburg.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).