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);
}