Version française
Home     About     Download     Resources     Contact us    
Browse thread
Concatenation of static strings?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ashish Agarwal <agarwal1975@g...>
Subject: Re: [Caml-list] Concatenation of static strings?
I was hoping there would be some follow up discussion on the code below, but
haven't seen anything yet. Can someone please clarify why this is not
considered a bug (or is it). Given that s is locally scoped within f, I do
not see why f returns different answers.

> # let f () = let s = "bla" in let c = s.[0] in s.[0] <- 'c' ; c ;;
> val f : unit -> char = <fun>
> # f () ;;
> - : char = 'b'
> # f () ;;
> - : char = 'c'



On Jan 19, 2008 5:55 AM, David Baelde <david.baelde@gmail.com> wrote:
> Nice, I didn't know about the stripping of the first whitespaces.
>
> Speaking of static strings, the static string allocation done by OCaml
> is not compatible with the mutability of strings. I've been told that
> the issue was raised a long time ago, so I'm not filing this as a bug,
> but since I could not find any information on the web I thought
> someone here might be able to recall what motivated the decision in
> former discussions. Maybe the issue is considered a little price to
> pay for the optimization, since we rarely use string mutations..
>
> The issue can be witnessed with the following code, on 3.10, either in
> the interactive loop or with any compiler:
>
> # let f () = let s = "bla" in let c = s.[0] in s.[0] <- 'c' ; c ;;
> val f : unit -> char = <fun>
> # f () ;;
> - : char = 'b'
> # f () ;;
> - : char = 'c'
>
> This is to be contrasted with arrays, which are mutable too but not
> statically allocated as for strings (let f () = let s =
> [|'b';'l';'a'|] in let c = s.(0) in s.(0) <- 'c' ; c).
>
> And for Erik, a test that tells us that concatenations are not done
statically:
>
> # let f () = let s = "b"^"la" in let c = s.[0] in s.[0] <- 'c' ; c ;;
> val f : unit -> char = <fun>
> # f () ;;
> - : char = 'b'
> # f () ;;
> - : char = 'b'
>
> Cheers,
>
> David
>
>
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>