Hi guys, apologies for the silly question, but I come from the world of dynamic languages and I'm wondering if it's possible to set struct values basic on dynamic variables?

e.g.

struct Person {
   string firstName;
   string lastName;
}

void main() {
   string[string] map;

   map["firstName"] = "Homer";
   map["lastName"] = "Simpson";
}

Is it possible to iterate through the hash map and set the struct values using the key/value pairs?

Something like this:

e.g.
Person p1;

foreach(key,val; map) {
   p1[key] = val;
}

The problem I'm trying to solve is that I'm wanting to write a generic "model" class that can read a record from a database and populate a known struct from the database values. I have seen the "allMembers" method from the traits module that can give me the names of the struct fields, but I am unsure if it's even possible to set the struct values using variable/dynamic names.

Any help will be greatly appreciated!
Cheers,
Andrew.

Reply via email to