Thread overview
Re: Why does 'string' alias immutable(char)[] and not immutable(char[]) ?
Dec 01, 2010
Jonathan M Davis
Dec 01, 2010
spir
Dec 01, 2010
Trass3r
December 01, 2010
On Wednesday 01 December 2010 03:17:48 spir wrote:
> Well, nearly everything is in the title... I would intuitively opt for the second type if I had to define an immutable string type.
> 
> Answers from various points of view (semantic, praticality, efficiency...) would be great if ever they may differ.

string str = "hello";
str ~	= " world";

That's wouldn't be possible if string were fully immutable. And since the fact that the elements of a string are immutable, passing a string to a function is guaranteed not to mess with the original, even if they append to the one passed in. So, making it fully immutable wouldn't really buy you anything anyway.

- Jonathan M Davis
December 01, 2010
On Wed, 1 Dec 2010 03:25:24 -0800
Jonathan M Davis <jmdavisProg@gmx.com> wrote:

> On Wednesday 01 December 2010 03:17:48 spir wrote:
> > Well, nearly everything is in the title... I would intuitively opt for the second type if I had to define an immutable string type.
> > 
> > Answers from various points of view (semantic, praticality, efficiency...) would be great if ever they may differ.
> 
> string str = "hello";
> str ~	= " world";
> 
> That's wouldn't be possible if string were fully immutable.

This, I do not understand. I thought immutable applies to the value, not to the variable. At least, this is what I know from other languages using immutable; but it seems, then, that this term does not mean the same in D. Immutable, for me, mean no reference to the value can change it. Thus, for instance:
    immutable(char[]) s = "abc";
    immutable(char[])* p = &s;
    *p = "def";     // Error: *p is not mutable

EDIT: All right, got it! I was caught once more by the fact that, for most types, in languages of the traditional imperative kind, a variable maps to a memory cell. Thus, immutability of the value also means "unchange-ability" of the variable. In other words:
    s = "def";      // Error: variable __trials__.main.s cannot modify immutable
because there is no way to change or replace s's value without mutating the original one. If I change s, then *p will see it. Is this reasoning correct?

(I was mislead because, in functional and dynamic languages from which I learnt the notion of immutability, elements are referenced, so that one can change a variable denoting an immutable value: it just silently points to another place in memory, but the original value never mutates. In such a language, changing s would not affect any other reference to the original value. Eg, python strings are immutable and referenced:
    s = "abc" ; t = s
    s = "def"
    print s,t		# "def abc"
)

> And since the fact that the elements of a string are immutable, passing a string to a function is guaranteed not to mess with the original, even if they append to the one passed in. So, making it fully immutable wouldn't really buy you anything anyway.

Yop.

> - Jonathan M Davis

Thank you, Jonathan.

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

December 01, 2010
> This, I do not understand. I thought immutable applies to the value, not to the variable.

You need to read from right to left.

immutable(char)[] is a mutable array of immutable chars.
immutable(char[]) is an immutable array of immutable chars. Note the transitivity.