digitalmars.D.learn - Heterogeneous CT array like structure
- Engine Machine (28/28) Aug 16 2016 struct Storage
- Adam D. Ruppe (47/50) Aug 16 2016 Just create an ordinary struct...
- Engine Machine (25/77) Aug 16 2016 This seems like a long winded way of just creating a struct in
- Adam D. Ruppe (7/11) Aug 16 2016 I literally said "Just create an ordinary struct..."
struct Storage { // Getter static auto ref opDispatch(string name)() { } // Setter static auto ref opDispatch(string name, T)(T val) { } } auto y = Storage.Data; Storage.Data = "x"; Storage.Foo = 3; How can I actually store this data for runtime use that doesn't require the GC? The indexing set is fixed at compile time. In fact, it could come from an enum(my first thought). The values for each index can have a different type, though, and I'd like them to be the proper types(no casting or variant). Is there a simple way to add to Storage at compile time? e.g., add static string Data = "x"; static int Foo = 3; The assignments are for default values, they will only occur once in program execution, what is important is the getter, which will create the data if it doesn't exist and set it to some default value initially if it needs to.
Aug 16 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); }
Aug 16 2016
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: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.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); }
Aug 16 2016
On Tuesday, 16 August 2016 at 19:31:02 UTC, Engine Machine wrote:This seems like a long winded way of just creating a struct in the first place, which might bet the simplest way anyways.I literally said "Just create an ordinary struct..." That's how I'd do it, though there are other options too.All this is just trying to be lazy. I could just edit a struct and add the variables as I create them.Editing a struct is probably the least-work option.... it is hard to get simpler than just adding "bool newOption;" to the one place (the surrounding arg handling, file load/saving, etc., can all be automated from the struct content.)
Aug 16 2016