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 MAA04250; Sat, 31 Jul 2004 12:02:21 +0200 (MET DST) 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 MAA03730 for ; Sat, 31 Jul 2004 12:02:20 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from m14s26.vip-server.net (cray.gaillourdet.net [80.190.204.60]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i6VA2JSH024637 for ; Sat, 31 Jul 2004 12:02:19 +0200 Received: from dijkstra.gaillourdet.net (m14s26.vip-server.net [80.190.204.60]) by m14s26.vip-server.net (Postfix) with ESMTP id 641E494CFF for ; Sat, 31 Jul 2004 12:01:02 +0200 (CEST) Received: by dijkstra.gaillourdet.net (Postfix, from userid 1000) id 60FA0585FB; Sat, 31 Jul 2004 11:24:07 +0200 (CEST) From: Jean-Marie Gaillourdert To: caml-list@inria.fr Subject: Re: [Caml-list] const equivalent for mutable types? Date: Sat, 31 Jul 2004 11:24:06 +0200 User-Agent: KMail/1.6.2 References: <410B5EBD.6060800@cgorski.org> In-Reply-To: <410B5EBD.6060800@cgorski.org> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200407311124.06799.jmg@gaillourdet.net> X-Miltered: at concorde with ID 410B6E2B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; jean-marie:99 caml-list:01 const:01 31.:99 2004:99 passing:01 reproducing:01 const:01 incr:01 printf:01 printf:01 incr:01 jean-marie:99 compiler:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, Am Samstag, 31. Juli 2004 10:56 schrieb Christopher A. Gorski: > In my code I find that I'm passing a lot of mutable values to functions. > Some functions merely read the values. Others modify the values. Is > there a method in OCaml for reproducing behavior similar in spirit to > the const declaration in C? In a purely functional language every parameter is "const". Although OCaml is not pure this behaviour is still the default. > Here is a specific case of the general problem: > > let t=ref 0 > let change r = incr r > let nochange r = Printf.printf "test:%d\n" !r > > The problem is that in complex programs I often get confused over what > functions are modifying values and what functions are not. I feel like > I should be able to do something like > > let result = change (const r) > > and have the compiler give me a type error. > > Is there a way to do this in OCaml? Should I change my programming > style? Am I asking a naive question that's already been answered many > times over in a different form? There is a very simple way to do so: Just don't pass the references around. let t=ref 0 let change r = incr r let nochange r = Printf.printf "test:%d\n" r You can now distinguish "const" parameters from "non-const" parameters. change t nochange !t Regards, Jean-Marie Gaillourdet ------------------- 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