Running mix gettext.extract results in a. Gettext "Hello people of Gotham! ", "fr " With strings as arguments: # in lib/greetings.ex Translations are just calls to gettext macros Gettext provides a mix gettext.extract task which is used to extract translations from source filesĪnd write them to. To use this technique while we were building I realized macros can be used to do compile-time work after José Valim proposed A real-world example (there is one this time!) Performance because, well, the compile-time logging code just disappears. Runtime: in fact, it won't leave a trace in the compiled code. Finally, we just return theĪrgument code (which is already an AST). Print this number: this is our compile time work. Sub-expressions by using Macro.prewalk/3. Say we have this code: defmodule MacroPhilosophy do def hello (name ) do "Hello # expressions/sub-expressions " However, we can also define functions in terms of macros: eachįunction is just a macro that does nothing at compile time. Return code instead of data in this description, we describe macros in terms ofįunctions. Usually, macros are described as functions that take code instead of data and Operation at compile time using this AST. Write macros that do not transform the AST they receive, but that perform some Turns out, we can take advantage of this! We can The code it returns, which is expanded recursively (searching for nested macros)īut not executed until runtime. Happens at compile time: a macro is executed at compile time and replaced with Macro call with the AST (the quoted code) it returns. Iex> ast |> Macro.expand(_ENV_) |> Macro.to_stringĮxpanding a macro means executing the code inside the macro and replacing the Seeing the expansion of this macro is trivial: iex> import SimpleMacro Macro so that our examples are straightforward: defmodule SimpleMacro do defmacro plus (x, y ) do quote do : unquote (x ) + unquote (y ) end end The key concept here is expansion: a macro call just gets transformed to If just expands to a case statement that checks whether the condition isįalse-y ( nil or false) or truthy (anything else), executing the correct block For example, theĭefinition of the if macro looks something like this: defmacro if (condition, do: do_block, else : else_block ) do quote do case unquote (condition ) do The representation of Elixir code) and transform it into new AST. Macros are often used as tools to manipulate the AST (Abstract Syntax Tree, Talk about another use case of macros that is rarely examined: doingĬompile-time things in macros. In thisĪrticle, I'll assume you are familiar with macros and how they work, and I'll Series of articles by Saša Jurić, and even aīook ( Metaprogramming Elixir) by Chris McCord. "Getting Started" guide on Elixir's website, an awesome Resources that explain what macros are and how to use them (much better than IĬould): there's the Macro chapter from the Macros are a very common way to do metaprogramming in Elixir.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |