Compile-time global mutables
Paul Backus
snarwin at gmail.com
Thu Jun 30 13:48:06 UTC 2022
On Thursday, 30 June 2022 at 13:39:57 UTC, HuskyNator wrote:
> On Thursday, 30 June 2022 at 13:16:48 UTC, Adam Ruppe wrote:
>> If you want to collect a bunch of things and load them at
>> once, you can use a module constructor; a `static this` in a
>> template and the compiler adds them all together.
>
> Not sure I see how that would apply to this example.
> How would you write a static constructor that loads in all
> files used somewhere in the source code? This seems like a
> prerequisite to being able to do that.
```d
string[] files;
template register(string file)
{
static this()
{
// TODO: actually load the file
files ~= file;
}
void register() {}
}
void foo()
{
register!"foo";
}
void bar()
{
register!"bar";
}
void main()
{
import std.stdio;
writeln(files); // ["foo", "bar"]
}
```
More information about the Digitalmars-d
mailing list