digitalmars.D - Const/Shared/Immutable anomalies in D that should be fixed
- Mehrdad (14/14) May 02 2012 I believe all of these static assertions (and some variants thereof) sho...
- Jonathan M Davis (10/28) May 02 2012 Marking a struct's definition as const or immutable just makes all of it...
- Mehrdad (2/5) May 02 2012 What sense does it make to have a struct whose members are all const, an...
- Chris Cain (20/27) May 03 2012 This doesn't even pass:
- Chris Cain (17/20) May 03 2012 Well, I just updated DMD to 2.059 (from 2.058) and lo and behold,
- Steven Schveighoffer (4/12) May 03 2012 Right, there is a common misconception that const can be implicitly cast...
- kenji hara (4/18) May 03 2012 It is bug 7038 and has been fixed in 2.059.
- deadalnix (3/5) May 06 2012 Const can point to mutable or immutable data.
- Mehrdad (4/10) May 06 2012 I think you misread my question.
- Mehrdad (4/17) May 06 2012 Actually, that brings up a good point -- it doesn't make a
- Steven Schveighoffer (5/8) May 07 2012 I missed this part of the question. If you haven't got an answer yet...
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?
May 02 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
May 02 2012
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.What sense does it make to have a struct whose members are all const, and which is not automatically const by itself?
May 02 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.
May 03 2012
On Thursday, 3 May 2012 at 07:45:53 UTC, Chris Cain wrote:The straightforward answer to this is that you really ought to have a main method :-) ...Well, I just updated DMD to 2.059 (from 2.058) and lo and behold, this doesn't pass for the new version. Interesting. And apparently static asserts can now occur outside of a scope (must have been a fixed bug). That said, I'm receptive to the change because I don't see a use case for the original behavior and I buy Mr. Davis' explanation. Plus this: void main() { immutable(Immutable) a; Immutable b; static assert(!is(typeof(a) == typeof(b)); } would fail with the old behavior, even though they are clearly distinct. And of course is(typeof(anImmutable) == shared) is also false with the new version.
May 03 2012
On Thu, 03 May 2012 03:45:51 -0400, Chris Cain <clcain uncg.edu> wrote:On Thursday, 3 May 2012 at 06:00:58 UTC, Mehrdad wrote:Right, there is a common misconception that const can be implicitly cast from shared, but it cannot. It's orthogonal. -SteveI believe all of these static assertions (and some variants thereof) should pass, due to the semantics of const, immutable, and shared. ... Do people agree?//static assert(is(Const == shared(Const))); // Doesn't pass
May 03 2012
It is bug 7038 and has been fixed in 2.059. http://d.puremagic.com/issues/show_bug.cgi?id=7038 Kenji Hara 2012/05/03 15:04 "Mehrdad" <wfunction hotmail.com>: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?
May 03 2012
Le 03/05/2012 08:00, Mehrdad a écrit :Also, what exactly is the difference between declaring a struct as immutable or as const? Aren't they unmodifiable either way?Const can point to mutable or immutable data. const are thread local and immutable aren't.
May 06 2012
I think you misread my question. I was asking about declaring a STRUCT as const, not a VARIABLE as const. On Sunday, 6 May 2012 at 13:09:06 UTC, deadalnix wrote:Le 03/05/2012 08:00, Mehrdad a écrit :Also, what exactly is the difference between declaring a struct as immutable or as const? Aren't they unmodifiable either way?Const can point to mutable or immutable data. const are thread local and immutable aren't.
May 06 2012
Actually, that brings up a good point -- it doesn't make a difference for the struct itself, but it makes a difference transitively... On Sunday, 6 May 2012 at 14:37:49 UTC, Mehrdad wrote:I think you misread my question. I was asking about declaring a STRUCT as const, not a VARIABLE as const. On Sunday, 6 May 2012 at 13:09:06 UTC, deadalnix wrote:Le 03/05/2012 08:00, Mehrdad a écrit :Also, what exactly is the difference between declaring a struct as immutable or as const? Aren't they unmodifiable either way?Const can point to mutable or immutable data. const are thread local and immutable aren't.
May 06 2012
On Sun, 06 May 2012 10:37:48 -0400, Mehrdad <wfunction hotmail.com> wrote:I think you misread my question. I was asking about declaring a STRUCT as const, not a VARIABLE as const.I missed this part of the question. If you haven't got an answer yet... const or invariant (or shared) applied to a struct is like applying it to every member of the struct. I think that's in the spec somewhere. -Steve
May 07 2012