caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* surprised by string_of_bool
@ 2008-05-27 23:10 Nathan Mishra Linger
  2008-05-28  8:02 ` [Caml-list] " David Allsopp
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Mishra Linger @ 2008-05-27 23:10 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 660 bytes --]

Consider the following transcript.

        Objective Caml version 3.10.0

# string_of_bool true == string_of_bool true;;
- : bool = true
# String.fill (string_of_bool true) 0 4 '@';;
- : unit = ()
# string_of_bool true;;
- : string = "@@@@"

Perhaps this has been discussed before, but I found this to be disturbing.
It seems easy enough to fix this problem, so that, for example, print_string
(string_of_bool true) always does the same thing.
The "correct" strategy seems to be used for string_of_float infinity:

# string_of_float infinity;;
- : string = "inf"
# string_of_float infinity == string_of_float infinity;;
- : bool = false

Nathan Mishra-Linger

[-- Attachment #2: Type: text/html, Size: 877 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [Caml-list] surprised by string_of_bool
  2008-05-27 23:10 surprised by string_of_bool Nathan Mishra Linger
@ 2008-05-28  8:02 ` David Allsopp
  2008-05-28  8:14   ` Jon Harrop
  0 siblings, 1 reply; 3+ messages in thread
From: David Allsopp @ 2008-05-28  8:02 UTC (permalink / raw)
  To: caml-list

> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] surprised by string_of_bool
  2008-05-28  8:02 ` [Caml-list] " David Allsopp
@ 2008-05-28  8:14   ` Jon Harrop
  0 siblings, 0 replies; 3+ messages in thread
From: Jon Harrop @ 2008-05-28  8:14 UTC (permalink / raw)
  To: caml-list

On Wednesday 28 May 2008 09:02:52 David Allsopp wrote:
> > 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 current implementation of string_of_float happens to allocate in this case 
but that is not necessary. It could just as well special case these special 
values and return static strings.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-05-28  8:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-27 23:10 surprised by string_of_bool Nathan Mishra Linger
2008-05-28  8:02 ` [Caml-list] " David Allsopp
2008-05-28  8:14   ` Jon Harrop

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).