caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: moj@utu.fi
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: immutable strings
Date: Wed, 07 Jun 2006 09:36:26 +0900 (JST)	[thread overview]
Message-ID: <20060607.093626.07649187.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20060605205433.GA14267@utu.fi>

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


      reply	other threads:[~2006-06-07  0:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-28 23:20 [Caml-list] Re: immutable strings (Re: Array 4 MB size limit) Harrison, John R
2006-05-29  2:36 ` Martin Jambon
2006-05-31 12:53 ` Jean-Christophe Filliatre
2006-06-05 20:54 ` immutable strings Matti Jokinen
2006-06-07  0:36   ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060607.093626.07649187.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=moj@utu.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).