📕 subnode [[@ryan/20210708160418 macros]]
in 📚 node [[20210708160418-macros]]
Macros are a feature in certain programming languagesthat allow for editing source code at compile or runtime. Similar to functions, macros are a means of code reuse, but rather than rewrite functionality they rewrite code.
Macros first appeared, to my knowledge, in Lisp
In Lisp (specifically Emacs Lisp, a macro looks like this:
(defmacro ++ (var) "Incrementing operator like in C." (list 'setq var (list '+ 1 var))) (let ((my-var 1)) (++ my-var))
2
The above, at runtime, is expanded in the following manner:
(macroexpand '(++ foo))
Consider also the following example in Rust
macro_rules! inc { ($name:ident) => { $name = $name + 1 } } fn main () { let mut foo = 1; inc!(foo); // macros in rust end with exclamation points println!("foo: {}", foo); // println is also a macro // => 2 }
Macros allow for programming language extension
📖 stoas
- public document at doc.anagora.org/20210708160418-macros
- video call at meet.jit.si/20210708160418-macros