Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 03, 2012 Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
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 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 03, 2012 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | > 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 03, 2012 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Cain | 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 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Cain | 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:
>> 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?
>
> //static assert(is(Const == shared(Const))); // Doesn't pass
Right, there is a common misconception that const can be implicitly cast from shared, but it cannot. It's orthogonal.
-Steve
|
May 03, 2012 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad Attachments:
| 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 06, 2012 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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 Re: Const/Shared/Immutable anomalies in D that should be fixed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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.
|
Copyright © 1999-2021 by the D Language Foundation