digitalmars.D.learn - Is this a bug in dmd 2.067 for struct initializers?
- stewarth (27/27) Feb 18 2015 Hi All,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/24) Feb 18 2015 In any case, that must be immutable(A)*.
- stewarth (13/38) Feb 19 2015 Hi Ali,
- Martin Nowak (3/4) Feb 22 2015 You should use shared static this to initialize immutable
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/8) Feb 22 2015 Is that because they are not thread-local? If so, initializing the same
- Jonathan M Davis via Digitalmars-d-learn (5/13) Feb 23 2015 Yeah. It really should be illegal to initialize immutable variables in
- amber (4/8) Feb 22 2015 OK, thanks a lot for the help.
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
Feb 18 2015
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
Feb 18 2015
On Thursday, 19 February 2015 at 07:46:51 UTC, Ali Çehreli wrote:On 02/18/2015 10:39 PM, stewarth wrote: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, StewThis 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
Feb 19 2015
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.
Feb 22 2015
On 02/22/2015 03:17 PM, Martin Nowak wrote:On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: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... AliI've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 22 2015
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: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 DavisOn Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote: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...I've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 23 2015
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:OK, thanks a lot for the help. Cheers, StewI've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 22 2015
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:Oops, replied under my friends account ... sorry for the confusion. I am assuming this is only if I have more than one thread? Thanks, StewOn Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote:OK, thanks a lot for the help. Cheers, StewI've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 22 2015
On Mon, 23 Feb 2015 02:15:08 +0000, stewarth wrote:On Monday, 23 February 2015 at 02:10:09 UTC, amber wrote:do not count on that. even if *you* never create additional threads, any=20 library can. it's better to always remember that you don't control all=20 execution pathes then be sorry later.=On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote:=20 Oops, replied under my friends account ... sorry for the confusion. =20 I am assuming this is only if I have more than one thread?On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote:OK, thanks a lot for the help. Cheers, StewI've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 22 2015
On Monday, 23 February 2015 at 04:04:08 UTC, ketmar wrote:On Mon, 23 Feb 2015 02:15:08 +0000, stewarth wrote:Ah yes of course. Good point and thanks for the help. StewOn Monday, 23 February 2015 at 02:10:09 UTC, amber wrote: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.On Sunday, 22 February 2015 at 23:17:33 UTC, Martin Nowak wrote:Oops, replied under my friends account ... sorry for the confusion. I am assuming this is only if I have more than one thread?On Thursday, 19 February 2015 at 22:07:55 UTC, stewarth wrote:OK, thanks a lot for the help. Cheers, StewI've gone with "static this()" approach and it works.You should use shared static this to initialize immutable variables.
Feb 22 2015