How to add an additional config dir in DUB under source?

Mike Parker aldacron at gmail.com
Tue Oct 13 05:13:18 UTC 2020


On Monday, 12 October 2020 at 22:31:53 UTC, tastyminerals wrote:

> I wonder why and what am I doing wrong?

This:

readText("conf.toml");

"stringImportPath" (dmd's -J command line option) is specifically 
for D's import expression (which is different from the import 
statement, e.g., `import std`):

https://dlang.org/spec/expression.html#import_expressions

It has no effect on arbitrary filesystem calls. And, in fact, 
filesystem calls cannot be called at compile-time because they 
ultimately depend on system calls for which the source code is 
not available. The very first restriction on CTFE is this:

"The function source code must be available to the compiler. 
Functions which exist in the source code only as extern 
declarations cannot be executed in CTFE."

So if you were to do something like, `enum foo = 
readText("foo.txt")`, you would get a compile time error. On 
Windows:

"Error: CreateFileW cannot be interpreted at compile time, 
because it has no available source code"

You're calling `readText` in a normal runtime context, so it's 
looking file at runtime relative to the current working directory.

What you need to do is something like this:

```
string config = import("config.toml");
void loadConfig() {
     // parse config string here
}
```

I don't know where the `EmbeddedPNG` template comes from, but it 
has to be using the import expression internally, else it 
wouldn't work.


More information about the Digitalmars-d-learn mailing list