immutable, const, enum

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 29 05:56:11 PDT 2009


On Wed, 29 Apr 2009 03:35:32 -0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Derek Parnell wrote:
>> Oh, I agree. It is just that the documentation is pretty adamant that
>> "immutability" means that the data WILL NOT change. It gives the  
>> impression
>> that there is compiler support to ensure that this is axiomatic rather  
>> than
>> just a convention.
>
> D is a systems programming language. By that I mean that there are  
> mechanisms to defeat the type system, for those that know what they're  
> doing.
>
> If you defeat the type system, and mutate immutably-typed data, the onus  
> is on you. It's like if you pop the carburetor off and put a  
> supercharger on, you void the warranty <g>.

I hate having to keep argue this, but you keep forgetting.  You cannot  
create immutable data without circumventing the type system (except  
strings which have special treatment).

Only way to make an immutable class:

auto c = cast(immutable(C))new C;

You have just circumvented the type system, and it's on you as a  
programmer to guarantee that C's constructor didn't store mutable  
references to itself somewhere, NOT the compiler.

Until this is fixed, immutability is far less useful (except for strings).

It's like you are buying a car where the dealer says you void the warranty  
if you put gas in it.

-Steve



More information about the Digitalmars-d mailing list