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

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.

More Fun

Write some quines.