Interested in D, spec confuses me.

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 2 13:56:04 PST 2016


On Tue, 02 Feb 2016 21:35:13 +0000, IceCreamEater wrote:

> On Tuesday, 2 February 2016 at 20:17:20 UTC, H. S. Teoh wrote:
>> On Tue, Feb 02, 2016 at 09:13:41PM +0100, anonymous via Digitalmars-d
>> wrote: [...]
>>> The other signature is no different. Two occurrences of "immutable",
>>> applying to two different things.
>>> 
>>> I agree that it can be unclear to newbies what exactly is immutable
>>> when a method is marked immutable, but the meaning of the keyword is
>>> the same as elsewhere. Using another word there would be more
>>> confusing.
>>
>> Another way to think about it, is that the "immutable" on the function
>> means that the implicit `this` reference to the object is immutable.
>>
>>
>> T
> 
> I thought immutable was added to the language as a better guarantee to
> 'const'. Which really tells me const wasn't cutting it and wasn't a
> proper guarantee.

D1 const (and up to about D2.007) was, if I recall, strictly for compile-
time constants.

When designing immutable, Bartosz and Walter realized that many functions 
don't care whether their parameters are mutable by anyone or not. For 
instance, the 'find' function in std.array -- it's not mutating the 
array, but it should be usable on mutable arrays.

So we got const, where all values implicitly cast to const. Functions 
promise not to modify anything marked const, so it's always safe to 
submit immutable values. They don't get access to potential optimizations 
available with immutable objects, which means they're safe for mutable 
values.


More information about the Digitalmars-d mailing list