From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id C72E1BBAF for ; Wed, 28 May 2008 10:03:09 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqMBAJSuPEjAXQImiGdsb2JhbACJVYhqAQEBDyCbaQ X-IronPort-AV: E=Sophos;i="4.27,553,1204498800"; d="scan'208";a="11209010" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 28 May 2008 10:03:09 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m4S839fM018817 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 28 May 2008 10:03:09 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqcBAFeuPEjC2fJWfGdsb2JhbACJVYhqAQELBQIGBxEDm2Y X-IronPort-AV: E=Sophos;i="4.27,553,1204498800"; d="scan'208";a="12816696" Received: from anchor-post-36.mail.demon.net ([194.217.242.86]) by mail1-smtp-roc.national.inria.fr with ESMTP; 28 May 2008 10:03:08 +0200 Received: from orion.metastack.com ([80.177.38.218]) by anchor-post-36.mail.demon.net with esmtp (Exim 4.67) id 1K1Gcm-0000on-KX for caml-list@inria.fr; Wed, 28 May 2008 08:03:08 +0000 Received: from countertenor (cpc1-cmbg6-0-0-cust264.cmbg.cable.ntl.com [81.101.137.9]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id m4S7tOGR027716 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Wed, 28 May 2008 08:55:25 +0100 From: "David Allsopp" To: References: Subject: RE: [Caml-list] surprised by string_of_bool Date: Wed, 28 May 2008 09:02:52 +0100 Organization: MetaStack Solutions Ltd. Message-ID: <71CA0AD3E2664A189B3B7DD8C603B1C0@countertenor> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AcjATd1tCo5VZxoaRkC6MKX/GP6yoQASExQg In-Reply-To: X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512 X-Scanned-By: MIMEDefang 2.63 on 172.16.28.218 X-Miltered: at discorde with ID 483D11BD.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; bool:01 bfd:01 ocaml:01 bool:01 mutation:01 buffer:01 literals:01 ocaml:01 ffi:01 inserting:01 disturbing:98 debe:98 imho:01 caml-list:01 caml-list:01 > Perhaps this has been discussed before, but I found this to be disturbing. http://caml.inria.fr/pub/ml-archives/caml-list/2007/06/bfd46e630ef150536b616 debe927384b.en.html on this and also http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/8158dd9e9581137f4bbcb 551e3ca6f3e.en.html on immutable strings in ocaml. > It seems easy enough to fix this problem, so that, for example, > print_string (string_of_bool true) always does the same thing. It depends on whether you regard it as a problem! IMHO if you write code that uses string mutation then you should be fully aware of where the allocation of the string that you're altering took place, just as you would with a buffer in C. If you're writing a library and are paranoid about string literals being altered then you just have to String.copy everything (I've used that before e.g. for the ocaml FFI in an ocaml-based interpreter for a language that has immutable strings only). The cost of having literal values freshly allocated on each function call (i.e. inserting String.copy before every non-format string literal) is a painful penalty for functional code that doesn't mutate strings. > The "correct" strategy seems to be used for string_of_float infinity: Well, it's just that string_of_bool can only return two possible values where string_of_float must perform an allocation each time. The declaration let string_of_bool x = String.copy (string_of_bool x);; would "fix" this function for you. David