On Tuesday, 16 August 2016 at 15:56:18 UTC, Adam D. Ruppe wrote:
On Tuesday, 16 August 2016 at 15:40:19 UTC, Engine Machine wrote:
How can I actually store this data for runtime use that doesn't require the GC?

The indexing set is fixed at compile time.

Just create an ordinary struct...


You could even create one  from code flow at compile time.



string magic(string structName) {
        import std.conv;
        struct Value {
                string name;
                string type;
                string value;
        }
        struct Magic {
                Value[] values;
                void opDispatch(string name, T)(T value) {
                        values ~= Value(name, T.stringof, to!string(value));
                }

                string toCodeString() {
                        string s;
                        s ~= "struct " ~ structName ~ " {\n";

                        foreach(value; values) {
                                s ~= "\t";

                                s ~= value.type;
                                s ~= " ";
                                s ~= value.name;
                                s ~= " = ";
                                if(value.type == "string")
                                        s ~= "`" ~ to!string(value.value) ~ "`";
                                else
                                        s ~= to!string(value.value);

                                s ~= ";\n";
                        }

                        s ~= "}";
                        return s;
                }
        }

        Magic magic;

        magic.a = 10;
        magic.b = "cool";

        return magic.toCodeString();
}

pragma( msg, magic("MagicStruct"));
mixin(magic("MagicStruct"));

void main() {
        MagicStruct ms;
        import std.stdio;
        writeln(ms.a);
        writeln(ms.b);
}

This seems like a long winded way of just creating a struct in the first place, which might bet the simplest way anyways.

What I am trying to do is create preferences for my app:

if (Preferences.EnableHQ)
 ...

But I don't want to have to define EnableHQ at the point of use! This may be impossible since D doesn't know the type, although I could do if (Preferences.EnableHQ!bool) or something like that.

Anyways, I want to just be able to create preferences on the fly in the code when I feel like I need one without having to do anything else like go edit a struct, etc. Then, later, I can go deal with that.

What I was thinking I could do is, is use opDispatch, write any calls to it to a file. Then use import to import that file in to the struct. It would be the data(like static bool EnableHQ = false).

Eventually opDispatch should never be called, which can easily be checked and I can import the file by handle and make changes if necessary.

The problem with this method is it requires multiple passes and not extensible.

Have any better ideas?

All this is just trying to be lazy. I could just edit a struct and add the variables as I create them.






Reply via email to