Version française
Home     About     Download     Resources     Contact us    
Browse thread
confusion about mutable strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ralph Douglass <ralph@g...>
Subject: Re: [Caml-list] confusion about mutable strings
Sorry, I should have made clear that this is not a problem I wanted solved
for me, but rather a question about OCaml.  I've just never come across this
before because I don't usually mutate strings.

Observe the following:

# let foo () =
  let bar = [|'a';'b';'c'|] in
  Array.iter (Printf.printf "%c") bar;
  bar.(0) <- 'd';
  bar;;
val foo : unit -> char array = <fun>
# foo ();;
abc- : char array = [|'d'; 'b'; 'c'|]
# foo ();;
abc- : char array = [|'d'; 'b'; 'c'|]

Why does OCaml treat these two examples in such a different manner?  Is
there a reason why strings are magically special in this way?

On 2/10/08, Bünzli Daniel <daniel.buenzli@erratique.ch> wrote:
>
> Each invocation of foo does not allocate a new string for str, "ffff"
> is a constant  string allocated once and you are updating this constant.
>
> let str = String.copy "ffff"
>
> will solve your problem.
>
> Best,
>
> Daniel
>
>


-- 
Ralph