Does D have a way to get and pass sturct information at runtime?

rempas rempas at tutanota.com
Mon Jun 16 07:30:31 UTC 2025


So, I wanted to create a library to allow D to be used a 
scripting language, in order to allow it to be used to configure 
programs. Now, the design of everything is rather simple. 
However, I do have one problem. Is there a way to pass runtime 
information of a struct, to the compiler that will compile my 
code? Let me show you an example:

```d
struct AppConfig {
    string name;
    u32 age;
}

void main() {
    CallLibIniti();

    // Pass the info of "AppConfig" so, the script file can use it 
without defining it
    auto script_file = 
CompileFile("$HOME/.config/my_app/config.d", AppConfig.info);

    // Create the config, using the "InitConfig" function from the 
script
    AppConfig config = 
cast(AppConfig)script_file.exec("InitConfig");
```

Configuration file path: `$HOME/.config/my_app/config.d`

```d
Config InitConfig() {
    Config config = { "CustomName", 255 };
    return config;
```



If what I want to do is not possible, I guess that another idea 
would be to get compile time information about a struct, write it 
to another file and automatically have the compiler add that file 
(if that's even possible)? Or maybe, modify the script file to 
add an **import** statement to it. Any ideas?


More information about the Digitalmars-d-learn mailing list