caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: <caml-list@inria.fr>
Subject: RE: [Caml-list] surprised by string_of_bool
Date: Wed, 28 May 2008 09:02:52 +0100	[thread overview]
Message-ID: <71CA0AD3E2664A189B3B7DD8C603B1C0@countertenor> (raw)
In-Reply-To: <ab0c618e0805271610sf3d1859w8fd51243b0aa598a@mail.gmail.com>

> 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


  reply	other threads:[~2008-05-28  8:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-27 23:10 Nathan Mishra Linger
2008-05-28  8:02 ` David Allsopp [this message]
2008-05-28  8:14   ` [Caml-list] " Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=71CA0AD3E2664A189B3B7DD8C603B1C0@countertenor \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).