Such "horror" stories are why I became disappointed in C and C++ - adding a new helpful feature into the language takes five years. The person who dared to propose this struggled and almost lost their hope.
Macros in Rust are a language feature. Rust doesn't have a separate preprocessor. User-defined macros operate on token streams, built-in macros can be hooked up to low-level compiler functionality. See include_bytes, which does exactly what embed does and whose implementation looks like
Because a regular function can't read a file at compile time, not in Rust or C or C++. They also can't return arbitrarily sized data on the stack in any of those languages.
what is blocking them though....my question is not about what we have rn but why we have it this way....why not a constexpr function in C++ or const eval function in Rust ....I'm assuming the return type can be a wrapper around something that's known at compile time....since the compiler will plug those bytes in anyways so the compiler knows the size..... what magic does macros have and const eval functions in Rust "cannot" ?
Macros can emit arbitrary code. Functions can only return predefined types with known sizes.
I'm assuming the return type can be a wrapper around something that's known at compile time....since the compiler will plug those bytes in anyways so the compiler knows the size.....
Are you suggesting a const eval function that returns CompilerByteLiteral or something? Because that's what a Rust macro is.
okay let me ask the question in reverse then....can I write a function that invokes this macro as a const eval function that return CompilerByteLiteral if not then why and if yes, then why not writing the function around the compiler built-in directly ?
i suggest you search for compile time reflection. It sounds like what you're asking for. It is proposed for c++, and if you want to test the idea out you can try it in java, it has both compile time and runtime reflection
84
u/spaun2002 Jul 23 '22
Such "horror" stories are why I became disappointed in C and C++ - adding a new helpful feature into the language takes five years. The person who dared to propose this struggled and almost lost their hope.