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 NAA00023; Thu, 6 Jun 2002 13:01:08 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA32746 for ; Thu, 6 Jun 2002 13:01:07 +0200 (MET DST) Received: from leia.mandrakesoft.com (office.mandrakesoft.com [195.68.114.34]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g56B17r25742 for ; Thu, 6 Jun 2002 13:01:07 +0200 (MET DST) Received: by leia.mandrakesoft.com (Postfix, from userid 505) id 4789944DB; Thu, 6 Jun 2002 12:58:58 +0200 (CEST) To: Winfried Dreckmann Cc: =?iso-8859-1?q?=80?= caml list Subject: Re: [Caml-list] look operator References: From: Pixel Date: 06 Jun 2002 12:58:58 +0200 In-Reply-To: Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Winfried Dreckmann writes: [...] > It's about abstracting the ! operator by introducing a function > > val look : tref -> t > > which coerces a mutable object into a non-mutable one. Using this function > is dangerous because, with this function, the non-mutable type t is not > strictly non-mutable anymore. As the manual says, the result of "look r" is > volatile, it is only guaranteed to be valid until the next in-place > operation involving r. In my own experience, mistakes occur faster than > expected. But this is a great and elegant trick. => "look" is only used for efficiency? couldn't the compiler achieve the same result without using an unsafe construct? > Using "look", every single > function with arguments of type t, say > > val add_in : tref -> t -> t -> unit, > > replaces two or more functions which would otherwise be necessary, in this > case > > val add_in1 : tref -> t -> t -> unit > val add_in2 : tref -> tref -> t -> unit > val add_in3 : tref -> tref -> tref -> unit are you saying that i would be nice to have this? As far as i have looked at numerix, it doesn't have this. > > at least. This would certainly blow up the library to impractical > dimensions. Of course, overloading would help, and "look" might become > obsolete in this way. > However, I think the problem is not mainly about overloading, agreed, row subtyping can already achieve this: ---------------------------------------- let add_in_wrapped r a b = r := !r + a + b let deref = function | `Ref a -> !a | `Const a -> a let add_in (`Ref a) b c = add_in_wrapped a (deref b) (deref c) let x = `Ref (ref 1) let y = `Ref (ref 2) let c = `Const 3 ;; add_in x y c ; x ---------------------------------------- > but about > reintroducing imperative features in an abstract and controlled way. I > could, for instance, also imagine an abstract assign operator > > val set : tref -> t -> unit > > where the contents of t is not copied but assigned to tref, and thus made > mutable, which could be useful in certain restricted ways. > > My question to the caml list: Would you accept such constructions as decent > Caml programming, if applied carefully and only in cases where it allows > what is otherwise impossible (e. g. integrating mutable and non-mutable > objects as it is done in "numerix"). Or is it all just a silent > reintroduction of C pointers, and principally a bad thing? I don't think this will never be in OCaml! (but i may be prooved wrong :) I've not found many information about this. AFAIK C++ is the only language having constness subtyping (http://merd.net/inoutness.html) The few links i've found: http://merd.net/inoutness.html#references I someone knows better, please tell, i'm interested :) ------------------- 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