Const/Shared/Immutable anomalies in D that should be fixed

Chris Cain clcain at uncg.edu
Thu May 3 00:45:51 PDT 2012


On Thursday, 3 May 2012 at 06:00:58 UTC, Mehrdad wrote:
> I believe all of these static assertions (and some variants 
> thereof) should pass, due to the semantics of const, immutable, 
> and shared.
>
> ...
>
> Do people agree?

This doesn't even pass:
static assert(is(Immutable == Immutable));

The straightforward answer to this is that you really ought to 
have a main method :-)

import std.stdio;

immutable struct Immutable {}
const struct Const {}
shared struct Shared {}

void main() {
     static assert(is(Immutable == immutable(Immutable)));
     static assert(is(Immutable == const(Immutable)));
     static assert(is(Immutable == shared(Immutable)));
     static assert(is(Const == const(Const)));
     //static assert(is(Const == shared(Const))); // Doesn't pass
     static assert(is(Shared == shared(Shared)));
}

> Also, what exactly is the difference between declaring a struct 
> as immutable or as const? Aren't they unmodifiable either way?

Since const != shared, that's probably the main difference. IMO, 
I'd probably never see a point to making a const struct, so I'd 
prefer to always declare it immutable.



More information about the Digitalmars-d mailing list