Thread overview
Does D have a way to get and pass sturct information at runtime?
6 days ago
rempas
6 days ago
rempas
6 days ago
Dejan Lekic
6 days ago
rempas
6 days ago

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:

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

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?

6 days ago

On Monday, 16 June 2025 at 07:30:31 UTC, rempas wrote:

>

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?

If what you desire is run-time configuration then the path forward is to invoke compiler say via std.process and compile your config to shared object. After that if compile is successful dlopen + dlsym is the way to go.

>

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?

There is import(“some_file”) to import files as data, but I’m not sure if that is what you are looking for.

6 days ago

On Monday, 16 June 2025 at 07:30:31 UTC, rempas wrote:

>

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

I assume you are not happy with RDMD ( https://dlang.org/rdmd.html ) ??
If my assumption is correct, may I ask why?

Or, do you want to have a REPL? If so, Martin Nowak implemented one many years ago: https://github.com/dlang-community/drepl . I honestly do not know in what state it is at the moment, have a look and see if it fits your needs.

6 days ago

On Monday, 16 June 2025 at 08:45:20 UTC, Dejan Lekic wrote:

>

I assume you are not happy with RDMD ( https://dlang.org/rdmd.html ) ??
If my assumption is correct, may I ask why?

Or, do you want to have a REPL? If so, Martin Nowak implemented one many years ago: https://github.com/dlang-community/drepl . I honestly do not know in what state it is at the moment, have a look and see if it fits your needs.

I... Didn't know about rdmd! Right now, I can't have a detailed look on it but, I reply to this comment to thank you beforehand! I'll come back and add another reply to let you know!

6 days ago

On Monday, 16 June 2025 at 08:41:57 UTC, Dmitry Olshansky wrote:

>

On Monday, 16 June 2025 at 07:30:31 UTC, rempas wrote:

>

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?

If what you desire is run-time configuration then the path forward is to invoke compiler say via std.process and compile your config to shared object. After that if compile is successful dlopen + dlsym is the way to go.

>

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?

There is import(“some_file”) to import files as data, but I’m not sure if that is what you are looking for.

Thanks for all that, they are all very interesting! Dejan Lekic told me about "rdmd" and I think it might actually be what I'm looking for. If it ends up been what I wanted, I guess I can build an sort of "wrapper" type of library around it, to make it a little easier to use and automate some things. I'll try it later when I find the time, and I'll let you all know!