3 ms·
Sounds like you want a struct except you want to be able to reflect on the property keys (which you can't do normally in C/C++/Rust)? I got thrown off by "but
by brundolf 6y ago
Sounds like you want a struct except you want to be able to reflect on the property keys (which you can't do normally in C/C++/Rust)?
I got thrown off by "but in an interpreted language" so I'm not sure whether you're requesting this for C or for another language. In JS, at least the V8 engine does some of this for you: if you have a group of objects that always have the same set of properties, V8 will detect this in many cases and optimize them under the hood as a struct or class (it must also store the string keys somewhere since those can be iterated in JS).
In C/C++ you'd have to store the keys as strings somehow, and also add some way to index the struct via those keys (since you can't even do that with a normal struct). You may be stuck with a hashmap one way or another.
- nicoburns 6y agoRust allows you to do this with a macro. The serde macros are pretty close to what you need, but a slight variant could give you a much more ergonomic API.
- brundolf 6y agoRight, I thought about suggesting a macro for C/C++ but I don't know enough about their macro systems to know for sure that it would work
- nicoburns 6y agoI believe C++ has "Runtime Type Information" (RTTI) that allows you to do this. Although I also hear that it's not great.
- MaxBarraclough 6y agoTo expand on not great: RTTI is one of the few features of C++ where you pay for it even if you don't use it, so it's not all that popular. It can be disabled with compiler flags to shrink the binary. LLVM's coding standard prohibits use of RTTI, they instead use their own 'hand-rolled' solution. [0] RTTI is also banned by the Google C++ coding standard. [1] [0] https://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or-exceptions https://llvm.org/docs/CodingStandards.html#do-not-use-rtti-o... [1] https://google.github.io/styleguide/cppguide.html#Run-Time_Type_Information__RTTI_ https://google.github.io/styleguide/cppguide.html#Run-Time_T...