www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static this not run?

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
I want a module level initialised delegate. if I try

module foo;
enum Status
{
      success,
}

class StatusException : Exception
{
      Status s;
      // usual exception constructors
}
void delegate(Status) onError = (Status s) { throw new 
StatusException(s);};

I get a error like cannot initialise something that needs a 
context at compile time. Ignoring the fact the the initialiser 
does not need a context, if I do

void delegate(Status) onError;
static this()
{
     onError = (Status s) { throw new StatusException(s);};
}

void main()
{
     import std.stdio;
     writeln(onError.funcptr); // null
}

WAT?

void delegate(Status) onError;
static this()
{
     import core.stdc.stdio;
     printf("initialising onError");
     onError = (Status s) { throw new StatusException(s);};
     printf("initialising onError: funcptr = 0x%x", 
onError.funcptr);
}

void main()
{
     import std.stdio;
     writeln(onError.funcptr);
}

output:
null

No "initialising onError", the static this is not even being run!
I'm using LDC master.

See also https://github.com/libmir/dcompute/issues/32
Sep 29 2017
parent reply user1234 <user1234 12.lo> writes:
On Saturday, 30 September 2017 at 06:15:41 UTC, Nicholas Wilson 
wrote:
 No "initialising onError", the static this is not even being 
 run!
 I'm using LDC master.

 See also https://github.com/libmir/dcompute/issues/32
LDC 1.4, DMD 2.076, DMD ~master and finally GDC all give the expected result here "initialising onErrorinitialising onError: funcptr = 0x<some address>" each time using: --- enum Status {success,} class StatusException : Exception { this(Status s) {this.s = s;super("");} Status s; } void delegate(Status) onError; static this() { import core.stdc.stdio; printf("initialising onError"); onError = (Status s) { throw new StatusException(s);}; printf("initialising onError: funcptr = 0x%x", onError.funcptr); } void main() { import std.stdio; writeln(onError.funcptr); } ---
Sep 30 2017
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 30 September 2017 at 08:49:14 UTC, user1234 wrote:
 On Saturday, 30 September 2017 at 06:15:41 UTC, Nicholas Wilson 
 wrote:
 No "initialising onError", the static this is not even being 
 run!
 I'm using LDC master.

 See also https://github.com/libmir/dcompute/issues/32
LDC 1.4, DMD 2.076, DMD ~master and finally GDC all give the expected result here "initialising onErrorinitialising onError: funcptr = 0x<some address>" each time using: --- enum Status {success,} class StatusException : Exception { this(Status s) {this.s = s;super("");} Status s; } void delegate(Status) onError; static this() { import core.stdc.stdio; printf("initialising onError"); onError = (Status s) { throw new StatusException(s);}; printf("initialising onError: funcptr = 0x%x", onError.funcptr); } void main() { import std.stdio; writeln(onError.funcptr); } ---
Hmm. Everything except for the main function was in a different module, I dont see why that would cause it to not be run, but then bugs have a tendency to do strange things like that. I'll have to dust mite DCompute and see what it gets as a minimal case.
Sep 30 2017
parent kinke <noone nowhere.com> writes:
On Saturday, 30 September 2017 at 12:07:21 UTC, Nicholas Wilson 
wrote:
 Hmm. Everything except for the main function was in a different 
 module, I dont see why that would cause it to not be run, but 
 then bugs have a tendency to do strange things like that. I'll 
 have to dust mite DCompute and see what it gets as a minimal 
 case.
[The object needs to be pulled in during linking.]
Sep 30 2017