From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA22246; Mon, 22 Mar 2004 20:09:40 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA28387 for ; Mon, 22 Mar 2004 20:09:38 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from mwinf0804.wanadoo.fr (smtp8.wanadoo.fr [193.252.22.23]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2MJ9bHd014900 for ; Mon, 22 Mar 2004 20:09:37 +0100 Received: from vanicat.homelinux.org (ca-bordeaux-24-62.w80-8.abo.wanadoo.fr [80.8.96.62]) by mwinf0804.wanadoo.fr (SMTP Server) with ESMTP id 6BBD31800045 for ; Mon, 22 Mar 2004 20:09:37 +0100 (CET) Received: from moi by vanicat.homelinux.org with local (Exim 4.30) id 1B5Unn-00015a-Ox for caml-list@inria.fr; Mon, 22 Mar 2004 20:09:35 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] Hashtbl and destructive operations on keys References: From: Remi Vanicat Mail-Copy-To: never Date: Mon, 22 Mar 2004 20:09:35 +0100 In-Reply-To: (Thomas Fischbacher's message of "Mon, 22 Mar 2004 19:16:57 +0100 (CET)") Message-ID: <871xnky9zk.dlv@vanicat.homelinux.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 hashtbl:01 physik:01 hashtbl:01 hash:01 val:01 induce:99 consing:01 val:01 abstr:01 abstr:01 hash:01 ocaml:01 ocaml:01 mutable:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 308 Thomas Fischbacher writes: > Dear ocaml hackers, > > I read the documentation in such a way that I must not assume that after > doing a Hashtbl.replace hash key new_val, I can destructively modify key > with impunity. (I do cons a new key at every Hashtbl.add.) > > On the other hand (I have not looked into the sources), I am quite > confident that the system _could_ give me the guarantee that > nothing evil happens if I do so, and especially for the application I am > presently working on, this would induce a noticeable performance gain, > due to reduced consing. (And performance is important here!) > > So, could I please get this officially sanctioned? :-) This is not an official answers, but it is what ocaml tell me : # let tbl = create 10;; val tbl : ('_a, '_b) Hashtbl.t = # let r = ref 10;; val r : int ref = {contents = 10} # replace tbl r 50;; - : unit = () # r := 1;; - : unit = () # find tbl r;; Exception: Not_found. So no, you can't modify in place the key without a danger. But : # class myref (x:int) = object val mutable value = x method set n = value <- n method get = value end;; class myref : int -> object val mutable value : int method get : int method set : int -> unit end # let tbl = create 10;; val tbl : ('_a, '_b) Hashtbl.t = # let r = new myref 10;; val r : myref = # replace tbl r 50;; - : unit = () # r #set 1;; - : unit = () # find tbl r;; - : int = 50 It do work for object because the hash value of object depend on an identifier that each object do have, and only of it. Two different object (even with same field) have a different id, and a object never change of id. The id can be none by using Oo.id. -- Rémi Vanicat ------------------- 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