On 2/23/20 6:55 AM, drathier wrote:
On Sunday, 23 February 2020 at 11:41:25 UTC, Johan Engelen wrote:
On Sunday, 23 February 2020 at 09:59:45 UTC, drathier wrote:
I'm having some trouble with the order in which `static this()` runs. This is the order defined in the source file, numbered for convenience:

To avoid confusion: you have all `static this()` in a single source file? Or across several source files?

-Johan

They're all in a single source file. The `[template]` prints are inside templates, like this:

```
template none(msg) {
     T!(msg)  none;
     static this() {
         none = ((std.functional.toDelegate(&batch!(msg) )))(X!(T!(msg) ));
     }
}
```

The whole reason I have `static this()` is to avoid ctfe crashing from trying to run `toDelegate` at compile time:

```
std/functional.d(1501,22): Error: dummyDel.funcptr cannot be evaluated at compile time
```

The static this is run in lexical order, but template static this are run as if they were stuck into the code at the first part they were instantiated.

So for instance, if you have:

template T() {
   int* T;
   static this() {
     T = new int;
   }
}

int x;
static this() {
   x = *T!();
}

The T instantiation comes AFTER the module-level static this. So it will run the module static this first, and then the T static this.

How to fix this? I tried putting the template into a separate module. Technically, the static constructor will be inserted into the instantiating module. But it does seem to order it properly in that case.

This is a limitation that I think D should be able to resolve.

I'll file an issue report.

-Steve

Reply via email to