On Sun, 2009-04-05 at 12:06 +0200, Zheng Li wrote:
> With phantom type alone, the abstraction can still leak.
Ok, it's actually not *immutable* strings but *read-only* strings. The
objective is to be able to distribute a text and make sure that the
client won't be able to modify it. The original owner can still decide
to enter "mutable-land" if they need it.
>
> ----
> # let ro_s = ro"asdf";;
> val ro_s : [ `Read ] Batteries.String.Cap.t = ro"asdf"
> # (to_string ro_s).[3] <- 'z';;
> ....
> ----
>
> however, with no covariants defined in batteries' String.Cap.t type
> (why?), the second example won't compile.
That's by design: [to_string] is the identity operation and it only
applies to strings for which you have both read and write capabilities.
If you wish to do what I believe you have in mind, you need to first
[copy] the string.
> The compiler simply doesn't
> allow me to print out a read-only string, nor does it allow many
> reasonable things like <<join ro"asdf" ro"jkl">> etc.
Er, how can you "not print out a read-only string"?
And for your [join] problem, well, works for me (as soon as you remember
that it takes as second argument a *list* of readable strings).
# print stdout (join ro";" [ro"1"; ro"2"; ro"3"]);;
1;2;3- : unit
Cheers,
David