caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: Thomas Fischbacher <t.fischbacher@soton.ac.uk>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Weird GC behaviour
Date: Wed, 28 Sep 2011 17:30:33 +0200	[thread overview]
Message-ID: <91C06ED2-1A8C-4F05-AEA7-ECB93A229113@inria.fr> (raw)
In-Reply-To: <4E830AC3.6020405@soton.ac.uk>

Dear Thomas,

Others have mostly answered your questions, but to be complete I want
to add the following:

> How come (Ftag "funny") is regarded as constant while
> (Rtag (ref "funny")) is not?

The difference here is that (ref "funny") is a function call.  The
compiler doesn't know that the "ref" function just allocates a record.

> After all, strings are mutable in OCaml,
> so there really is not that much of a conceptual difference between a
> string and a string ref in that respect:

You should rather look at the analogy between strings and arrays, but
as Gabriel said, string literals are very rarely mutated in practice,
so it makes more sense to have "funny" return the same string instead
of allocating.

> But the problem I think I have with OCaml is: there just seems to be no
> way to properly express the conceptual difference between '(1 2 3 4 5)
> and (list 1 2 3 4 5): All I can say above is: Ftag "Hello".

You should say (Ftag (String.copy "Hello")) if you want a fresh mutable
string.  I wouldn't recommend appending "" or using Obj.dup (yuck!)

I agree it was a mistake to make strings mutable, but we have to live
with it for the time being.  If you want to be perfectly safe, you can
wrap all string literals with String.copy in your program.

-- Damien


  parent reply	other threads:[~2011-09-28 15:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-27 17:30 Thomas Fischbacher
2011-09-28 11:19 ` Damien Doligez
2011-09-28 11:53   ` Thomas Fischbacher
2011-09-28 12:12     ` Gabriel Scherer
2011-09-28 13:07     ` John Carr
2011-09-28 15:30     ` Damien Doligez [this message]
2011-09-28 23:32       ` Philippe Wang

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=91C06ED2-1A8C-4F05-AEA7-ECB93A229113@inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=t.fischbacher@soton.ac.uk \
    /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).