arcadian

arcadian

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.

First 9 of 9 Posts Switch mode

joey_the_snake

joey_the_snake

cloudytoday

cloudytoday

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.

BartOtten

BartOtten

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

Sebb

Sebb

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

D4no0

D4no0

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

joey_the_snake

joey_the_snake

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.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

al2o3cr

al2o3cr

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.

kip

kip

ex_cldr Core Team

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement