English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
RE: [Caml-list] Re: immutable strings (Re: Array 4 MB size limit)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-06-07 (00:34)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Re: immutable strings
From: moj@utu.fi (Matti Jokinen)

> > In practice, the obvious library calls are safe, so like Aleksey, I use
> > the built-in strings for the sake of convenience and compatibility. But
> > it's unsatisfactory intellectually.
> Actually, there are cases of unsafe sharing even in the standard library.
> # let x = "X" in
>   let g = Genlex.make_lexer [x] in
>   let s = Stream.of_string "X" in
>   let t = g s in
>   let _ = Stream.peek t in
>   x.[0] <- 'Y';
>   Stream.peek t;;
> result:
> - : Genlex.token option = Some (Genlex.Kwd "Y")
> I think this demonstrates that the problem is real: it is too easy to
> forget copying.

I don't think this is what the original poster meant by "unsafe".
Standard library functions do not mutate strings when this is not
explicitly stated.
If you apply this principle to user behaviour, it means that you
shouldn't mutate a string passed to or from a library function except
when it is explicitly ok.

In practice this usually works well, because the string type is
actually used as two independent types:
* mutable strings for some I/O and buffers
* immutable strings for all other uses

But this still puts a burden on users.

Jacques Garrigue