static this not run?

Nicholas Wilson iamthewilsonator at hotmail.com
Sat Sep 30 06:15:41 UTC 2017


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


More information about the Digitalmars-d-learn mailing list