The World Is Your Playground

Metaprogramming in Elixir is all about extensibility. Have you ever wished your favorite language would adopt that one neat feature? If you’re lucky, it might take years to happen. Often it never happens at all. In Elixir, you can introduce new first-class features at will. Take the familiar while loop that you find in most languages. It’s missing from Elixir, but you can imagine writing one like this:

 
while Process.alive?(pid) ​do
 
send pid, {self, :ping}
 
receive​ ​do
 
{^pid, :pong} -> IO.puts ​"Got pong"
 
after​ 2000 -> break
 
end
 
end

In the next chapter, we make this while loop a reality. It doesn’t stop there, though. With Elixir, we can define languages with the language, to express all kinds ...

Get Metaprogramming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.