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.
Here are questions you can ask, and things you can do, that fall under the umbrella of metaprogramming:
Here are some examples in no particular order.
Python has good metaprogramming support.
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 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.
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 doesn’t really have the same kind of metaprogramming facilities you see in other dyanmic languages. It has some support for reflection via interface{}
.
As a language in the Lisp family, Clojure’s metaprogramming facilities are strong.
Julia features Lisp-like (and Clojure-like) macros, built from abstract syntax trees rather than source code text.
Looking for some fun? Write some quines.
We’ve covered: