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

Jonathan M Davis jmdavisProg at gmx.com
Wed May 2 23:12:46 PDT 2012


On Wednesday, May 02, 2012 23:00:57 Mehrdad wrote:
> I believe all of these static assertions (and some variants thereof) should
> pass, due to the semantics of const, immutable, and shared.
> 
> immutable struct Immutable { }
> const struct Const { }
> shared struct Shared { }
> 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)));
> static assert(is(Shared == shared(Shared)));
> 
> 
> Do people agree?
> 
> Also, what exactly is the difference between declaring a struct as immutable
> or as const? Aren't they unmodifiable either way?

Marking a struct's definition as const or immutable just makes all of its 
members const or immutable. The type itself can't be const or immutable. A 
_variable_ of that type could be const or immutable (and then const or 
immutable is part of the type of the variable), but the struct type itself 
can't be const or immutable.

Those assertions will fail.

Also, while immutable is implicitly shared, it _isn't_ shared, so asserting 
that it is will fail.

- Jonathan M Davis


More information about the Digitalmars-d mailing list