Metaprogramming is the design of programs that generate code meant to be used at the same of a similarly high level of abstraction as the metaprogram’s language. For example, the popular languages Python and Javascript each provide a built-in function called eval that takes as input a string and then tries to interpret that string as a piece of code in the respective language (here, “interpret” refers to immediately executing the code a high level rather than first compiling that code to a low-level representation, usually C or Assembly).

Metaprogramming is the ultimate abstraction tool, since (almost) any pattern of code at all can be generated by a metaprogram of the same or smaller size (asymptotically, at the very least). However, metaprogramming can also yield some severe side-effects.

  1. Generated code is not directly visible while programming, so there is an additional layer of obfuscation between programming and feedback.
  2. Metaprograms usually only have limited access to static information, which inferred by the compiler, about the programs to be generated. On top of this, metaprograms are not usually checked to make sure they generate valid programs. Python and Javascript’s eval function know that their argument is a string and nothing more! 3.

Dependent Types

Metaprogramming via Dependent Types