Heterogeneous CT array like structure
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 16 08:56:18 PDT 2016
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);
}
More information about the Digitalmars-d-learn
mailing list