Custom Language Constructs
You’ve seen that macros allow you to effectively create your own keywords in the language, but they also allow Elixir to be flexible against future requirements. For example, instead of waiting for the language to add a parallel for comprehension, you could extend the built-in for macro with a new para macro that spawns processes to run the comprehensions in parallel. It could look something like this:
| para(for i <- 1..10 do: i * 10) |
If implemented, para would transform the for AST into code that runs the comprehension in parallel. The original code would gain just one natural para invocation while executing the built-in comprehension in an entirely new way. José gave us a solid language foundation that we can ...
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.