dirEntries within static foreach: memory.d(827): Error: fakePureErrno cannot be interpreted at compile time, because it has no available source code

kdevel kdevel at vogtner.de
Mon Feb 11 00:19:02 UTC 2019


I am trying to get this code compiled:

```TemplateStore.d
module TemplateStore;
import std.path;
import std.conv;
import std.file;

immutable string[string] template_map;

static this ()
{
    static foreach (f; dirEntries (``, `*.html`, 
SpanMode.shallow)) {
       pragma (msg, `reading template <` ~ f ~ ">");
       template_map[f] = import (f);
    }
}
```

dmd v2.082.0 says

...linux/bin64/../../src/druntime/import/core/memory.d(827): 
Error: fakePureErrno cannot be interpreted at compile time, 
because it has no available source code
Error: invalid foreach aggregate <error>
Error: invalid foreach aggregate <error>

If I use an immutable array of file names instead of dirEntries 
the code compiles, but I don't want to update that array each 
time a new template file is added.

As a workaround I wrote a helper which writes the quoted 
filenames comma-separated into a file named `LS` using this 
fragment:

    foreach (f; dirEntries (``, `*.html`, SpanMode.shallow))
       writefln ("`%s`,", f);

Then I try to convince the dmd compiler to read LS and use its 
content as initializer for the immutable array template_files:

    immutable LS = import (`LS`);
    pragma (msg, `LS = <` ~ LS ~ `>`);
    immutable template_files = [
       mixin(LS)
    ];

dmd now says (1) if the list in LS has a trailing comma:

    TemplateStore.d-mixin-13(26): Error: expression expected, not 
End of File

or (2) if the list has no trailing comma:

    TemplateStore.d-mixin-13(13): Error: Using the result of a 
comma expression is not allowed
    TemplateStore.d-mixin-13(14): Error: Using the result of a 
comma expression is not allowed
    TemplateStore.d-mixin-13(15): Error: Using the result of a 
comma expression is not allowed
    :

What can I do about it?







More information about the Digitalmars-d-learn mailing list