Re: copying const
On Wednesday, 24 June 2015 at 19:45:12 UTC, Adam D. Ruppe wrote: On Wednesday, 24 June 2015 at 19:41:54 UTC, Freddy wrote: I was surprised when this happened, is it a bug or a feature? Feature, overwriting const or immutable data would violate the promise that it never changes. Saving a struct over another struct is just another way of overwriting the data. This is why you should almost never have structs with const or immutable members. It's overly limiting. Once one member is const or immutable, the whole thing might as well be, and if that's what you want, just declare the variable const or immutable and don't have individual members be const or immutable. If read-only is what you're looking for, then wrap it in an @property function which returns the value but don't provide a corresponding setter property, and then the only way to overwrite it is to assign a value to the entire struct. Classes are different, since they're reference types, but with structs, I'm not sure I've ever seen a case where I thought that it was a good idea to have any of its members be const or immutable. - Jonathan M Davis
copying const
I was surprised when this happened, is it a bug or a feature? --- struct A { const int var; } int test() { auto b = A(5); auto c = A(7); pragma(msg, typeof(b)); //A pragma(msg, typeof(c)); //A auto d = b; //compiles b = c; //doesn't compile } --- $ rdmd -main -unittest test A A test.d(13): Error: cannot modify struct b A with immutable members Failed: [dmd, -main, -unittest, -v, -o-, test.d, -I.]
Re: copying const
On Wednesday, 24 June 2015 at 19:41:54 UTC, Freddy wrote: I was surprised when this happened, is it a bug or a feature? Feature, overwriting const or immutable data would violate the promise that it never changes. Saving a struct over another struct is just another way of overwriting the data.
Re: copying const struct
On Monday, 14 October 2013 at 18:36:00 UTC, Maxim Fomin wrote: Funniest thing happen in cases like this when he reports the bug himself and soon fixes it, later bug is hit by someone else but during reporting it is realized that bug is already fixed, pull is pushed to master and everyone salutes Kenji. Obviously Kenji has a time machine. With it, he discovers bugs in the future and fixes them in the present and sometimes even in the past.
Re: copying const struct
Am 14.10.2013 13:35, schrieb Jack Applegame: Why this doesn't compile? http://dpaste.dzfl.pl/21ef5b04 class Foo {} struct Bar1 { const(Foo[]) member; } struct Bar2 { const Foo member; } void main() { const Bar1 bar1; const Bar2 bar2; Bar1 b1 = bar1; // ok Bar2 b2 = bar2; // cannot implicitly convert expression (bar2) of type const(Bar2) to Bar2 } Because in D const is transitive. That means if a reference is const the object it points to is also const. And everything that object points to is const, and so on. The line Bar2 b2 = bar2; would remove const from member which does not happen because const is not convertible to mutable implicitly. Kind Regards Benjamin Thaut
copying const struct
Why this doesn't compile? http://dpaste.dzfl.pl/21ef5b04 class Foo {} struct Bar1 { const(Foo[]) member; } struct Bar2 { const Foo member; } void main() { const Bar1 bar1; const Bar2 bar2; Bar1 b1 = bar1; // ok Bar2 b2 = bar2; // cannot implicitly convert expression (bar2) of type const(Bar2) to Bar2 }
Re: copying const struct
On Monday, 14 October 2013 at 11:38:23 UTC, Benjamin Thaut wrote: The line Bar2 b2 = bar2; would remove const from member Seems strange, because b2.member is const by declaration.
Re: copying const struct
On Monday, 14 October 2013 at 11:45:30 UTC, Jack Applegame wrote: On Monday, 14 October 2013 at 11:38:23 UTC, Benjamin Thaut wrote: The line Bar2 b2 = bar2; would remove const from member Seems strange, because b2.member is const by declaration. Strange indeed and smells like a bug. `auto bar = Bar2(bar2.member)` works so it should be assignable.
Re: copying const struct
Am 14.10.2013 13:54, schrieb Dicebot: On Monday, 14 October 2013 at 11:45:30 UTC, Jack Applegame wrote: On Monday, 14 October 2013 at 11:38:23 UTC, Benjamin Thaut wrote: The line Bar2 b2 = bar2; would remove const from member Seems strange, because b2.member is const by declaration. Strange indeed and smells like a bug. `auto bar = Bar2(bar2.member)` works so it should be assignable. d.puremagic.com/issues/show_bug.cgi?id=9665
Re: copying const struct
On Monday, 14 October 2013 at 11:35:32 UTC, Jack Applegame wrote: Why this doesn't compile? http://dpaste.dzfl.pl/21ef5b04 class Foo {} struct Bar1 { const(Foo[]) member; } struct Bar2 { const Foo member; } void main() { const Bar1 bar1; const Bar2 bar2; Bar1 b1 = bar1; // ok Bar2 b2 = bar2; // cannot implicitly convert expression (bar2) of type const(Bar2) to Bar2 } This is compilable using git head.
Re: copying const struct
On Monday, 14 October 2013 at 13:56:58 UTC, Benjamin Thaut wrote: Am 14.10.2013 13:54, schrieb Dicebot: On Monday, 14 October 2013 at 11:45:30 UTC, Jack Applegame wrote: On Monday, 14 October 2013 at 11:38:23 UTC, Benjamin Thaut wrote: The line Bar2 b2 = bar2; would remove const from member Seems strange, because b2.member is const by declaration. Strange indeed and smells like a bug. `auto bar = Bar2(bar2.member)` works so it should be assignable. d.puremagic.com/issues/show_bug.cgi?id=9665 I don't see how this is related to the topic.
Re: copying const struct
Am 14.10.2013 16:19, schrieb Maxim Fomin: On Monday, 14 October 2013 at 13:56:58 UTC, Benjamin Thaut wrote: Am 14.10.2013 13:54, schrieb Dicebot: On Monday, 14 October 2013 at 11:45:30 UTC, Jack Applegame wrote: On Monday, 14 October 2013 at 11:38:23 UTC, Benjamin Thaut wrote: The line Bar2 b2 = bar2; would remove const from member Seems strange, because b2.member is const by declaration. Strange indeed and smells like a bug. `auto bar = Bar2(bar2.member)` works so it should be assignable. d.puremagic.com/issues/show_bug.cgi?id=9665 I don't see how this is related to the topic. I thought maybe its related to this bug.
Re: copying const struct
On Monday, 14 October 2013 at 14:18:10 UTC, Maxim Fomin wrote: On Monday, 14 October 2013 at 11:35:32 UTC, Jack Applegame wrote: Why this doesn't compile? http://dpaste.dzfl.pl/21ef5b04 class Foo {} struct Bar1 { const(Foo[]) member; } struct Bar2 { const Foo member; } void main() { const Bar1 bar1; const Bar2 bar2; Bar1 b1 = bar1; // ok Bar2 b2 = bar2; // cannot implicitly convert expression (bar2) of type const(Bar2) to Bar2 } This is compilable using git head. Recently I found a small compiler bug. http://d.puremagic.com/issues/show_bug.cgi?id=11187 And, a week ago it is fixed. 2.064 beta1 contains the fix. Kenji Hara
Re: copying const struct
On 10/14/2013 10:33 AM, Kenji Hara wrote: On Monday, 14 October 2013 at 14:18:10 UTC, Maxim Fomin wrote: This is compilable using git head. When I read that line ... Kenji Hara ... I was thinking about that person. :) Thank you Kenji! Ali
Re: copying const struct
On Monday, 14 October 2013 at 17:38:22 UTC, Ali Çehreli wrote: On 10/14/2013 10:33 AM, Kenji Hara wrote: On Monday, 14 October 2013 at 14:18:10 UTC, Maxim Fomin wrote: This is compilable using git head. When I read that line ... Kenji Hara ... I was thinking about that person. :) Thank you Kenji! Ali Right. Kenji is true D celebrity. Funniest thing happen in cases like this when he reports the bug himself and soon fixes it, later bug is hit by someone else but during reporting it is realized that bug is already fixed, pull is pushed to master and everyone salutes Kenji. What bothers me is that some (in fact, probably many) delicate language cases are silently fixed by Kenji without proper documentation so D language has some sparse and obsolete spec, even more obsolete book and sporadic patches - just couple of developers are aware about each of them. When someone hits corner language case - weird compiler behavior it is general unknown what should compiler do, whether current behavior is bug or a fix for some other previous bug about which you are unaware of. When different parts of language interact, problem becomes even more complicated.