how would D be different if string were const(char)[]?
Jonathan M Davis
jmdavisProg at gmx.com
Thu Oct 17 09:38:21 PDT 2013
On Thursday, October 17, 2013 15:18:19 Dicebot wrote:
> On Thursday, 17 October 2013 at 13:08:18 UTC, Daniel Davidson
>
> wrote:
> > If it would be no different then why prefer immutable(char)[]
> > for string?
>
> Allocation-free slicing would have been illegal/unsafe then as
> someone could have possibly modified underlying chars via mutable
> reference.
It wouldn't have to be illegal, and there's nothing unsafe about it. You can
slice fully mutable arrays as much as you like without allocating any memory,
and it's perfectly safe. However, you can't rely on the elements of a const
array not being modified (because a mutable reference to the same data could
modify it), and you can't share it across threads (as unlike immutable, const
is not implicitly shared). So, dealing with strings if they were const(char)[]
would probably be more bug-prone, and you'd be forced to dup your strings far
more often in order to guarantee that they weren't being altered by a mutable
reference somewhere. On top of that, you really wouldn't gain anything. So, it
would definitely be a bad idea. Defaulting to immutable(char)[] makes a lot of
sense, and you can still use const(char)[] or char[] if you want to, so it's
not like it's particularly limiting either.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list