www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Const/Shared/Immutable anomalies in D that should be fixed

reply "Mehrdad" <wfunction hotmail.com> writes:
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
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
parent "Mehrdad" <wfunction hotmail.com> writes:
 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
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
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
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
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
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
prev sibling next sibling parent kenji hara <k.hara.pg gmail.com> writes:
--90e6ba308ef2e4b82604bf256abe
Content-Type: text/plain; charset=ISO-8859-1

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?

--90e6ba308ef2e4b82604bf256abe Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <p>It is bug 7038 and has been fixed in 2.059.<br> <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D7038">http://d.p= uremagic.com/issues/show_bug.cgi?id=3D7038</a></p> <p>Kenji Hara</p> <div class=3D"gmail_quote">2012/05/03 15:04 &quot;Mehrdad&quot; &lt;<a href= =3D"mailto:wfunction hotmail.com">wfunction hotmail.com</a>&gt;:<br type=3D= "attribution"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;= border-left:1px #ccc solid;padding-left:1ex"> I believe all of these static assertions (and some variants thereof) should= pass, due to the semantics of const, immutable, and shared.<br> <br> immutable struct Immutable { }<br> const struct Const { }<br> shared struct Shared { }<br> static assert(is(Immutable =3D=3D immutable(Immutable)));<br> static assert(is(Immutable =3D=3D const(Immutable)));<br> static assert(is(Immutable =3D=3D shared(Immutable)));<br> static assert(is(Const =3D=3D const(Const)));<br> static assert(is(Const =3D=3D shared(Const)));<br> static assert(is(Shared =3D=3D shared(Shared)));<br> <br> <br> Do people agree?<br> <br> Also, what exactly is the difference between declaring a struct as immutabl= e or as const? Aren&#39;t they unmodifiable either way? <br> </blockquote></div> --90e6ba308ef2e4b82604bf256abe--
May 03 2012
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
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
next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
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
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
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
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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