From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id B1E11BC84 for ; Mon, 14 Mar 2005 11:31:12 +0100 (CET) Received: from daimi.au.dk (daimi.au.dk [130.225.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2EAVBhQ011879 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 14 Mar 2005 11:31:12 +0100 Received: from [10.11.41.210] (arwen [10.11.41.210]) (authenticated bits=0) by daimi.au.dk (8.12.11/8.12.11) with ESMTP id j2EAUvO0001061; Mon, 14 Mar 2005 11:30:57 +0100 Message-ID: <423567E1.1030909@sophia.inria.fr> Date: Mon, 14 Mar 2005 11:30:57 +0100 From: Pascal Zimmer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041020 X-Accept-Language: fr, fr-fr, en MIME-Version: 1.0 To: Oliver Bandel Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] immutable Strings? References: <20050312205011.GA2244@first.in-berlin.de> In-Reply-To: <20050312205011.GA2244@first.in-berlin.de> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-DAIMI-Spam-Score: 0 () X-Scanned-By: MIMEDefang 2.44 X-Miltered: at nez-perce with ID 423567EF.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 oliver:01 bandel:01 mutable:01 mutable:01 ocaml:01 datatype:01 sig:01 val:01 val:01 char:01 struct:01 abstr:01 char:01 ...:98 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Oliver Bandel wrote: > for records it is possible to say "mutable" to change a normally > non mutable value into a mutable one. > > It would be nice to have the possibility to turn on > immutability fpr strings with a keyword like "immutable" > or so. > > So it could be forbidden to modify strings in cases, > where it makes sense; otherwise it must be provided > String.copy at a lot of cases in a program, that > want's to forbid modyfiing the original data. > > And disallowing a modification is much stronger/more strict > than only allowing modyfiing a copy. > So, it would be nice to have such a feature in newer versions > of OCaml. You can achieve the same effect quite easily using modules to provide a new interface to String, where you restrict the set of possible operations and abstract the datatype: module type IMMSTRING = sig type t (* abstract type *) val create : string -> t val copy : t -> t val copy_to_string : t -> string val get : t -> int -> char val cat : t -> t -> t val print_string : t -> unit (* ... *) end;; module ImmString : IMMSTRING = struct type t = string let create = String.copy let copy = String.copy let copy_to_string = String.copy let get s i = s.[i] let cat s t = s ^ t let print_string = print_string (* ... *) end;; Now you can write: # let s = ImmString.create "abcde";; val s : ImmString.t = # ImmString.get s 0;; - : char = 'a' # s.[0] <- 'f';; This expression has type ImmString.t but is here used with type string The bad points: - you cannot use the shortcuts s.[i] and (^) anymore (this gets the code less readable especially for the first one) - you have to make a copy when creating an immutable string; if you were not planning to keep the original string to modify it, this copy is in fact useless Pascal