From: Pascal Zimmer <Pascal.Zimmer@sophia.inria.fr>
To: Oliver Bandel <oliver@first.in-berlin.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] immutable Strings?
Date: Mon, 14 Mar 2005 11:30:57 +0100 [thread overview]
Message-ID: <423567E1.1030909@sophia.inria.fr> (raw)
In-Reply-To: <20050312205011.GA2244@first.in-berlin.de>
Oliver Bandel wrote:
> for records it is possible to say "mutable" to change a normally
> non mutable value into a mutable one.
>
> It would be nice to have the possibility to turn on
> immutability fpr strings with a keyword like "immutable"
> or so.
>
> So it could be forbidden to modify strings in cases,
> where it makes sense; otherwise it must be provided
> String.copy at a lot of cases in a program, that
> want's to forbid modyfiing the original data.
>
> And disallowing a modification is much stronger/more strict
> than only allowing modyfiing a copy.
> So, it would be nice to have such a feature in newer versions
> of OCaml.
You can achieve the same effect quite easily using modules to provide a
new interface to String, where you restrict the set of possible
operations and abstract the datatype:
module type IMMSTRING =
sig
type t (* abstract type *)
val create : string -> t
val copy : t -> t
val copy_to_string : t -> string
val get : t -> int -> char
val cat : t -> t -> t
val print_string : t -> unit
(* ... *)
end;;
module ImmString : IMMSTRING =
struct
type t = string
let create = String.copy
let copy = String.copy
let copy_to_string = String.copy
let get s i = s.[i]
let cat s t = s ^ t
let print_string = print_string
(* ... *)
end;;
Now you can write:
# let s = ImmString.create "abcde";;
val s : ImmString.t = <abstr>
# ImmString.get s 0;;
- : char = 'a'
# s.[0] <- 'f';;
This expression has type ImmString.t but is here used with type string
The bad points:
- you cannot use the shortcuts s.[i] and (^) anymore (this gets the code
less readable especially for the first one)
- you have to make a copy when creating an immutable string; if you were
not planning to keep the original string to modify it, this copy is in
fact useless
Pascal
next prev parent reply other threads:[~2005-03-14 10:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-03-12 20:50 Oliver Bandel
2005-03-14 10:30 ` Pascal Zimmer [this message]
2005-03-14 10:41 ` [Caml-list] " Jon Harrop
2005-03-14 11:15 ` Pascal Zimmer
2005-03-14 12:57 ` Richard Jones
2005-03-14 13:01 ` Richard Jones
2005-03-15 2:13 ` Jacques Garrigue
2005-03-15 8:09 ` Oliver Bandel
2005-03-15 8:40 ` Oliver Bandel
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=423567E1.1030909@sophia.inria.fr \
--to=pascal.zimmer@sophia.inria.fr \
--cc=caml-list@yquem.inria.fr \
--cc=oliver@first.in-berlin.de \
/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