Are there accessible metaprogramming examples?

I have been reading “Polished Ruby Programming” by Jeremy Evans and am really enjoying it. He has one chapter, chapter 9, on Metaprogramming, and another, chapter 10, on DSLs.

His examples are quite accessible, like for example he starts by creating a class where, when you call it with a missing method and argument to evaluate, it uses missing_method to actually create a method returning that result. So write myclass.foo 4 + 8, and from then on myclass.foo will evaluate to 12.

There is also another example - quite funny IMO - where he creates a module named “Rusty” where you can define functions with fn instead of def, and do it in one line. So within that module you can write fn :rand, “Time.now.usec/1000000.0”, and when you call myclass.rand you get a psuedo-random number based on the time fraction.

And another example where he uses method missing at the kernel level to give ruby an always accessible words(x y z) function that returns an array of those words as symbols, i.e. [:x :y :z], although admittedly that one looked like dark magic to me.

I was wondering whether elixir had any examples like this, since I know that the language was influenced by Ruby and has similar metaprogramming capabilities. Ultimately I would like to understand how modules that create their own DSLs function, like for example how Phoenix does its routing via the module in the router.ex file.

1 Like

The best two resources I know (besides just diving into someone else’s codebase):

6 Likes

To add to the above, official guides on macros are decent for getting going.

See the very end of the side bar for the next two chapters.
You can also just google stuff, in my experience it’s quite accessible and sometimes can even be useful.
Then, official documentation will come very handy.

4 Likes

Addendum: Read the book before the Erlangelist posts. The former is a very gentle introduction and will show you many tecniques (for your examples). The latter goes deep really fast (but goes to (dark) places the book nevers goes) :slight_smile:

But yeah, those two are indeed all you need

4 Likes

the book is 8 years old, is it still valid?

The Metaprogramming Elixir very much, I always tend to return to it when I forget how to do some shenanigans in my macros.

1 Like

I’m not sure what you mean by valid. If you mean completely up to date with any new things they added since then, then no. If you want to know the fundamentals then yes.

Off the top of my head I can’t really think of anything that has been added on the macro front honestly.

1 Like

I could see books that predate Code.quoted_to_algebra having to spend a lot of effort making AST → code transformations, but that’s more “macro-adjacent” than macro-related.

1 Like

Metaprogramming in Elixir is more than macros. Arguably the non-macro parts are more accessible, easier to understand and less potentially obfuscating so I typically recommend starting there. And yes, Chris’s book is great (first Elixir book I bought way back when).

Its also a testament to José’s original language design that the AST hasn’t fundamentally changed. Some changes to the metadata to support improvements to the overall developer experience but that’s about it.

4 Likes