3 ms·
it's "meta" programming, since Lisp macros do source transformations: code to new code. So one writes code which will rewrite code. For example some programming
by lispm 2y ago
it's "meta" programming, since Lisp macros do source transformations: code to new code. So one writes code which will rewrite code. For example some programming language lacks a control structure one would want. Instead of waiting for the benevolent dictator for life implementing this feature, one can do it by creating a macro, which implements the control structure.
For example, imagine that you want to program with state machines and you need a short notation for that in your programming language. In Lisp you could design a syntax for a state machine and the Lisp macro would transform state machine descriptions into the code used to implement them -> the generated code typically will be longer and full of implementation details -> in the state machine description one would only specify what's necessary. The Lisp macro will do the code transformation from using the new control structure to the implementation of the control structure.
Thus one can view Lisp as a programmable programming language.
- gpderetta 2y agoTemplates are not nearly as powerful as lisp macros and stitching together code is 100 time more complex (bordering on Turing tarpit territory), but almost arbitrary compile time code generation is still possible. See boost.lambda, boost.spirit, eigen or anything using expression templates for example. Aside the complexity, the main thing C++ currently lacks is code introspection to modify existing code without using an ad-hoc DSL. I think that even the reflection proposals do not go as far as actually introspecting code.
- lispm 2y agoCan a template access a database/filesystem/network at compile time? Any idea about that?
- jandrewrogers 2y agoThe proposals and features around std::embed and #embed (part of C23) are on their way to enabling this.