Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005493OCamlOCaml generalpublic2012-01-26 10:342013-08-31 12:46
Assigned Tolefessan 
StatusclosedResolutionnot fixable 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0005493: Pervasives.string_of_bool doesn't create new string
Descriptionfeast:trunk john$ ocaml
        Objective Caml version 3.12.1

# let x = string_of_bool false;;
val x : string = "false"
# x.[3] <- 'x';;
- : unit = ()
# x;;
- : string = "falxe"
# string_of_bool false;;
- : string = "falxe"

Is this the intended behaviour? It seems to break abstraction somewhat.

Solution would be to use string_blit and string_create from to build a new string each time. I would be happy to write the patch if required.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
protz (manager)
2012-01-26 10:37

Other funny example:

# let f () = "toto";;
val f : unit -> string = <fun>
# let v = f () in v.[1] <- 'a';;
- : unit = ()
# f ();;
- : string = "tato"

There was a discussion about that on the caml-list, you may be able to find it by crawling the archives of the google group. The tl;dr is that strings are optimized, and shared; therefore, you shouldn't think of them as something mutable.

I'm inclined to close this as "not a problem" but I'll wait for others to weigh in...
johnwhitington (reporter)
2012-01-26 10:56

I guess that makes sense. I just found it confusing on a fundamental API boundary. Perhaps it deserves mention in the documentation?
protz (manager)
2012-01-26 11:04

Well I guess a small disclaimer somewhere near the documentation of the [String] module would be worthwhile. I'll check with others see if we should recommend some sort of rope library for efficient sharing...
lefessan (developer)
2012-01-26 11:27

I changed the documentation of Pervasives.string_of_bool to state that the returned value may be shared and should not be modified directly.

A lot of functions that return strings should actually be documented in the same way, for example when some of them may return the value received as argument when no modification is required.

Users should use String.copy to guarantee that they are no modifying a shared string.
protz (manager)
2012-01-30 16:34

r12102 also adds an extra comment in string.mli about that "feature"

- Issue History
Date Modified Username Field Change
2012-01-26 10:34 johnwhitington New Issue
2012-01-26 10:37 protz Note Added: 0006804
2012-01-26 10:56 johnwhitington Note Added: 0006805
2012-01-26 11:04 protz Note Added: 0006806
2012-01-26 11:27 lefessan Note Added: 0006807
2012-01-26 11:27 lefessan Status new => resolved
2012-01-26 11:27 lefessan Resolution open => not fixable
2012-01-26 11:27 lefessan Assigned To => lefessan
2012-01-30 16:34 protz Note Added: 0006846
2013-08-31 12:46 xleroy Status resolved => closed

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker