Metaprogramming

Meta-what?

What is it?

Metaprogramming is writing programs that operate on other programs. So writing:

is metaprogramming. But the fun kind of metaprogramming is writing a program operates on itself.

If a program simply looks at and reports on itself, we call this introspection. If the program also modifies itself, we call this reflection.

Typical Operations

Here are questions you can ask, and things you can do, that fall under the umbrella of metaprogramming:

Examples

Here are some examples in no particular order.

Python

Python has good metaprogramming support.

Ruby

Ruby just may win for the most metaprogramming-friendly and metaprogramming-rich language of them all. There are entire multi-week courses and whole books devoted to Ruby metaprogramming.

JavaScript

JavaScript is already very dynamic; objects are really just dictionaries so accessing properties by name is just normal programming. It does have eval and an interesting constructor for Function. It has a cool Reflect object.

Read this Hacker News discussion to see some strong opinions on what is and what is not metaprogramming in JavaScript.

Java

You might think that Java, because it is so static and so verbose, couldn’t have too many metaprogramming features. But that is so not the case. It has annotations. It has a reflection package. It has a lot more.

Go

Go doesn’t really have the same kind of metaprogramming facilities you see in other dyanmic languages. It has some support for reflection via interface{}.

Clojure

As a language in the Lisp family, Clojure’s metaprogramming facilities are strong.

Julia

Julia features Lisp-like (and Clojure-like) macros, built from abstract syntax trees rather than source code text.

Something Related

Looking for some fun? Write some quines.

Recall Practice

Here are some questions useful for your spaced repetition learning. Many of the answers are not found on this page. Some will have popped up in lecture. Others will require you to do your own research.

  1. What is metaprogramming?
    Writing programs that operate on other programs.
  2. What are some programs that count as metaprograms?
    Compilers, assemblers, interpreters, linkers, loaders, debuggers, profilers.
  3. What is introspection?
    A program looking at and reporting on itself.
  4. What is reflection?
    A program modifying itself.
  5. As a relatively static language, Go doesn’t have as rich of metaprogramming facilities as Ruby. But it does have one interesting metaprogramming mechanism. What is it?
    Reflection.
  6. What are some mainstream languages with macros implemented via abstract syntax trees?
    Clojure and Julia.
  7. What is quine?
    A program that prints its own source code.

Summary

We’ve covered:

  • What metaprogramming is
  • Examples of metaprogramming
  • Various uses in several programming languages
  • Quines for fun