Ever dream of having that one cool feature in your language of choice? With luck, you might see it in a few years, more likely never. However, Elixir is different. Through metaprogramming, Elixir allows you to extend the language and create first-class features.
1/17
Most programming languages have an AST (Abstract Syntax Tree), but it's hidden from you. When compiled or interpreted, the program's source code is transformed into a tree structure before being turned into bytecode or machine code.
2/17
@josevalim, the creator of Elixir, chose to expose the AST and provide a natural syntax for interacting with it. Now as an Elixir developer, you have the power to operate on a level usually reserved for compilers and language designers.
3/17
Metaprogramming revolves around inspecting and manipulating the AST. For example, the Logger module in Elixir optimizes performance by removing expressions from the AST. Most languages would instead waste resources checking ignored logger levels.
4/17
Rather than work with the AST directly, Elixir provides macros to empower you to work with the AST with natural Elixir syntax and constructs.
Macros receive ASTs as arguments and return ASTs as values. Macros are essentially code that generates code.
5/17
Macros allow you to pattern match on the AST structure passed into it.
defmacro add({:+, _, [lhs, rhs]})
6/17
the quote macro allows you to return an AST. the unquote macro allows values to be injected into the AST that's being defined. It's like string interpolation for code. You use it whenever evaluating a passed argument.
7/17
Putting that all together, here's an example macro that matches on a simple 2 term addition statement.
8/17
Within a module, macros can have multiple function signatures. So in the example above, you could also implement a `say` macro for multiplication.
9/17
There are two rules for writing macros. 1. Don't write them 2. Write them gratuitously. Keep this duality in mind as you explore macros. Many have been bitten by reckless complexity, but you must also avoid letting pitfalls scare you away from the power of macros.
10/17
Every expression in Elixir breaks down to a three-element tuple in the AST. {atom, metadata, arguments}. The AST forms trees of functions and arguments.
11/17
For example, in your iex shell you can run
quote do: 2 + 2
> {:+, [context: Elixir, import: Kernel], [2, 2]}
quote do: 2 + 2 + 2
{:+, _, [{:+, _, [2, 2]}, 2]}
I've simplified metadata [context: Elixir, import Kernel] to _ for the sake of readability.
12/17
In macro expansion, the compiler encounters a macro and then expands it. if the expanded code also contains macros it then expands those. This expansion occurs recursively until all macros have been expanded into fully generated code.
13/17
Macros inject code for the caller. The place where the code is injected is called a context. A context is the scope of the caller's bindings, imports, and aliases. If you lose track of your macro's execution context, it's a sign your code generation may be too complex.
14/17
Macro hygiene protects the caller's context. Variables, imports, and aliases that you define in a Macro do not bleed into the caller's own definitions.
15/17
You can use the var! macro to explicitly allows the macro to override hygiene and produce AST that has the caller's bindings. var! can be a necessary evil but should be avoided when possible.
16/17
I hope you enjoyed today's summary! I'm happy to be back and doing these again. I've been prioritizing work on the upcoming #elixirnewbie website, but I hope to be back to doing these daily :)
17/17
• • •
Missing some Tweet in this thread? You can try to
force a refresh
Macros can be used for powerful code generation. They can save time, reduce boilerplate, and produce elegant APIs. To realize the power of macros, remember that most of the Elixir Standard library is implemented as macros.
1/11
You have the freedom to extend the language and implement your own desired features. Macros allow you to create your own keywords and allow Elixir to be flexible for future requirements.
Erlang is a general-purpose development platform that supports concurrency, scalability, fault-tolerance, distribution, and high availability. It was built in the 1980s by Ericcson, a Swedish telecom giant. However, it is not specialized for telecom systems.
1/17
Erlang powers large applications such as WhatsApp, the Riak distributed database, the Heroku cloud, the Chef deployment automation system, and the RabbitMQ message queue.
Raspberry pi is a great way to get your start in IoT, and Nerves and Livebook take that even further. Cool enough: the first real Elixir code Lars wrote was for the Rasberry Pi Zero.
1/13
You can sandwich a Rasberry Pi and an eInk display together. The Rasberry Pi runs your application and your application can use The Libraries Inky and Chisel to write to the display.
Bottom line, before the end of September, if you are on Elixir OTP 23 or 24 you should upgrade to the latest patch.
specifically OTP 23.3.4.5 or OTP 24.0.4
If you are on an OTP version older than 23, the good news is you will be unaffected so long as you do not upgrade your dependencies. You can freeze your dependencies in order to buy yourself time to upgrade to OTP 23 or 24.
Error handling can be tricky, and can cause a task you would expect to take a single day to instead take a whole week!
1/10
This episode is focused on dealing with errors caused by working with external services. Anytime you're dealing with external dependency, you expose yourself to risk.
By far the most loved library on the Panel today was Telemetry. Telemetry powers monitoring and observability in elixir projects and provides a consistent interface to do so. Other mentions were OTP, Livebook, and site_encrypt.
1/24
When members of today's panel came into the Elixir Industry, they found opinions they like and trust from the Elixir community but also questioned established norms. Thus today's topic: Code Heresy.