#+title: macros Macros are a feature in certain [[file:20210307152738-programming_languages.org][programming languages]] that 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 [[file:20200604222651-lisp.org][Lisp]]. In Lisp (specifically [[file:20200713103540-emacs_lisp.org][Emacs Lisp]]), a macro looks like this: #+begin_src emacs-lisp (defmacro ++ (var) "Incrementing operator like in C." (list 'setq var (list '+ 1 var))) (let ((my-var 1)) (++ my-var)) #+end_src #+RESULTS: : 2 The above, at runtime, is expanded in the following manner: #+begin_src emacs-lisp :results value raw (macroexpand '(++ foo)) #+end_src #+RESULTS: (setq foo (+ 1 foo)) Consider also the following example in [[file:20200705154112-rust.org][Rust]]: #+begin_src rust :eval never 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 } #+end_src Macros allow for [[file:20210708161728-programming_language_extension.org][programming language extension]].