Re: Is this a bug in dmd 2.067 for struct initializers?
On Sunday, February 22, 2015 17:45:48 Ali Çehreli via Digitalmars-d-learn wrote: On 02/22/2015 03:17 PM, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. Is that because they are not thread-local? If so, initializing the same variable in multiple 'static this()' blocks would indeed be a race condition. Very important... Yeah. It really should be illegal to initialize immutable variables in non-shared static constructors. https://issues.dlang.org/show_bug.cgi?id=4923 - Jonathan M Davis
Re: Is this a bug in dmd 2.067 for struct initializers?
On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables.
Re: Is this a bug in dmd 2.067 for struct initializers?
On Monday, 23 February 2015 at 04:04:08 UTC, ketmar wrote: On Mon, 23 Feb 2015 02:15:08 +, stewarth wrote: On Monday, 23 February 2015 at 02:10:09 UTC, amber wrote: On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. OK, thanks a lot for the help. Cheers, Stew Oops, replied under my friends account ... sorry for the confusion. I am assuming this is only if I have more than one thread? do not count on that. even if *you* never create additional threads, any library can. it's better to always remember that you don't control all execution pathes then be sorry later. Ah yes of course. Good point and thanks for the help. Stew
Re: Is this a bug in dmd 2.067 for struct initializers?
On 02/22/2015 03:17 PM, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. Is that because they are not thread-local? If so, initializing the same variable in multiple 'static this()' blocks would indeed be a race condition. Very important... Ali
Re: Is this a bug in dmd 2.067 for struct initializers?
On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. OK, thanks a lot for the help. Cheers, Stew
Re: Is this a bug in dmd 2.067 for struct initializers?
On Mon, 23 Feb 2015 02:15:08 +, stewarth wrote: On Monday, 23 February 2015 at 02:10:09 UTC, amber wrote: On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. OK, thanks a lot for the help. Cheers, Stew Oops, replied under my friends account ... sorry for the confusion. I am assuming this is only if I have more than one thread? do not count on that. even if *you* never create additional threads, any library can. it's better to always remember that you don't control all execution pathes then be sorry later. signature.asc Description: PGP signature
Re: Is this a bug in dmd 2.067 for struct initializers?
On Monday, 23 February 2015 at 02:10:09 UTC, amber wrote: On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote: On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: I've gone with static this() approach and it works. You should use shared static this to initialize immutable variables. OK, thanks a lot for the help. Cheers, Stew Oops, replied under my friends account ... sorry for the confusion. I am assuming this is only if I have more than one thread? Thanks, Stew
Re: Is this a bug in dmd 2.067 for struct initializers?
On Thursday, 19 February 2015 at 07:46:51 UTC, Ali Çehreli wrote: On 02/18/2015 10:39 PM, stewarth wrote: This works under dmd 2066.1 but fails under dmd 2.067-b2. I don't know whether it is a bug. struct B { A* a; In any case, that must be immutable(A)*. } static immutable B[] someB = [{a:someA[0]}, {a:someA[1]}]; I want it to initialize at runtime before main(). I don't actually want any CTFE stuff here. Then you need 'static this()' (or 'shared static this()'): static immutable B[] someB; static this() { someB = [ B(someA[0]), B(someA[1]) ]; } Note that I could not use the named member syntax because in my case the compiler cannot know what the right-hand side expression is. However, it is still possible with a temporary where the type is explicit as in your case: static this() { immutable B[] tmp = [ {a:someA[0]}, {a:someA[1]} ]; someB = tmp; } Ali Hi Ali, Thanks for the help. I've gone with static this() approach and it works. In a way it's cleaner because it's explicit that these variables are only initialised at runtime before d_main(). At least that's how I understand things :) It would be nice if the named syntax also worked in static this(), maybe I'll file an ER for it. I'm a big fan of the whole named args thing in Python, which from a quick search has been discussed before in the forums. Cheers, Stew
Re: Is this a bug in dmd 2.067 for struct initializers?
On 02/18/2015 10:39 PM, stewarth wrote: This works under dmd 2066.1 but fails under dmd 2.067-b2. I don't know whether it is a bug. struct B { A* a; In any case, that must be immutable(A)*. } static immutable B[] someB = [{a:someA[0]}, {a:someA[1]}]; I want it to initialize at runtime before main(). I don't actually want any CTFE stuff here. Then you need 'static this()' (or 'shared static this()'): static immutable B[] someB; static this() { someB = [ B(someA[0]), B(someA[1]) ]; } Note that I could not use the named member syntax because in my case the compiler cannot know what the right-hand side expression is. However, it is still possible with a temporary where the type is explicit as in your case: static this() { immutable B[] tmp = [ {a:someA[0]}, {a:someA[1]} ]; someB = tmp; } Ali
Is this a bug in dmd 2.067 for struct initializers?
Hi All, This works under dmd 2066.1 but fails under dmd 2.067-b2. --- struct A { int val; } static immutable A[] someA = [{val:1}, {val:2}]; struct B { A* a; } static immutable B[] someB = [{a:someA[0]}, {a:someA[1]}]; void main() { writefln(a:%s, someA); writefln(b:%s, someB); } --- Error under DMD 2.067-b2: hack.d(27): Error: cannot use non-constant CTFE pointer in an initializer '[A(1), A(2)][0]' hack.d(27): Error: cannot use non-constant CTFE pointer in an initializer '[A(1), A(2)][1]' Is this a bug, or a deliberate change? I'm thinking a bug because I want it to initialize at runtime before main(). I don't actually want any CTFE stuff here. Thanks, stew