caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Aleksey Nogin <aleksey@nogin.org>
To: caml-list@inria.fr
Subject: [Caml-list] 3.07+2 - Constants immediatelly disappear from the Weak array?
Date: Wed, 12 Nov 2003 01:13:48 -0800	[thread overview]
Message-ID: <3FB1F9CC.8070607@nogin.org> (raw)

When trying to switch from 3.06 to 3.07+2 I've noticed the following 
difference in how the Weak module works:

----

         Objective Caml version 3.07+2

# type 'a anchored_entry = { anchor : 'a; index: int };;
type 'a anchored_entry = { anchor : 'a; index : int; }
# let memo = Weak.create 34;;
val memo : '_a Weak.t = <abstr>
# let make =
      let count = ref (-1) in
         fun data ->
            incr count;
            Weak.set memo !count (Some data);
            { anchor = data; index = !count };;
val make : '_a -> '_a anchored_entry = <fun>
# let test ae =
      match Weak.get memo ae.index with
         Some data -> if ae.anchor != data then invalid_arg "Got a copy" 
else data
       | None -> invalid_arg "Entry went away";;
val test : '_a anchored_entry -> '_a = <fun>
# test (make []);;
Exception: Invalid_argument "Entry went away".
# test (make [1]);;
- : int list = [1]

----

         Objective Caml version 3.06

# type 'a anchored_entry = { anchor : 'a; index: int };;
type 'a anchored_entry = { anchor : 'a; index : int; }
# let memo = Weak.create 34;;
val memo : '_a Weak.t = <abstr>
# let make =
      let count = ref (-1) in
         fun data ->
            incr count;
            Weak.set memo !count (Some data);
            { anchor = data; index = !count };;
val make : '_a -> '_a anchored_entry = <fun>
# let test ae =
      match Weak.get memo ae.index with
         Some data -> if ae.anchor != data then invalid_arg "Got a copy" 
else data
       | None -> invalid_arg "Entry went away";;
val test : '_a anchored_entry -> '_a = <fun>
# test (make []);;
- : '_a list = []
# test (make [1]);;
- : int list = [1]

----

Basically, in 3.07+2, the empty list disappears from the weak array as 
soon as it is added into it. Is this a bug or a feature?

The old behavior is useful because it allows to assume that while a 
value is referenced somewhere, Weak.get will always return Some. This 
allowed using Weak.get as an indicator of whether it was OK to discard 
some "helper" data (which should be only discarded after the primary 
data is no longer in use).

I've reported this to http://caml.inria.fr/bin/caml-bugs - see #1925

-- 
Aleksey Nogin

Home Page: http://nogin.org/
E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal)
Office: Jorgensen 70, tel: (626) 395-2907

-------------------
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:[~2003-11-12  9:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=3FB1F9CC.8070607@nogin.org \
    --to=aleksey@nogin.org \
    --cc=caml-list@inria.fr \
    /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).