caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] 3.07+2 - Constants immediatelly disappear from the Weak array?
@ 2003-11-12  9:13 Aleksey Nogin
  0 siblings, 0 replies; only message in thread
From: Aleksey Nogin @ 2003-11-12  9:13 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-11-12  9:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-12  9:13 [Caml-list] 3.07+2 - Constants immediatelly disappear from the Weak array? Aleksey Nogin

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).