Chapter 1 of Metaprogramming Elixir, Write Less Code, Get More Done (and Have Fun!) by @chris_mccord

You can get your copy here:
pragprog.com/titles/cmelixi…

Twitter summaries are back! 🧵

#myelixirstatus #elixirnewbie.

0/17
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
 

Keep Current with Brooklin Myers

Brooklin Myers Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @BrooklinJMyers

26 Oct
Chapter 2 of Metaprogramming Elixir by @chris_mccord

You can get a copy here:
pragprog.com/titles/cmelixi…

Summarizing so you can get the key takeaways! 🧵

#elixirnewbie #myelixirstatus

0/11
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.

2/11
Read 12 tweets
22 Sep
Today's summary comes from the book Elixir in Action. by @sasajuric.

manning.com/books/elixir-i…

What is Erlang?🧵0/17

#myelixirstatus #elixirnewbie
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.

2/17
Read 18 tweets
16 Sep
Todays summary: Watched @lawik's LiveStream: Video: Nerves, Livebook & small displays (eInk, OLED)
underjord.io/livestream-ner…

How @NervesProject helps you get started with Elixir and IoT without needing to know a lot about the hardware.

🧵0/13

#myelixirstatus #elixirnewbie
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.

2/13
Read 14 tweets
15 Sep
Listened to @ThinkingElixir with @cadebward @bernheisel and @brainlid and guest @voltonez

OTP Certificates Woes with Bram Verburg. spoti.fi/3luRtVs

On September 30, 2021, the root CA certificate DST Root CA X3 will expire. Here's how you stay safe. 🧵

#myelixirstatus
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.
Read 5 tweets
14 Sep
Today's summary thread! @ElixirOutlaws with @Adkron, and @ChrisKeathley. Episode 101: Bright and Tight.

open.spotify.com/episode/6YW6Qb…

This episode was packed with information on handling errors in Elixir.

#elixirstatus #elixirnewbie

🧵0/10
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.

2/10
Read 11 tweets
13 Sep
Listened to @BeamRadio1 Episode 18: Code Heresy with Chris Keathley.

Today's panel: @lawik @akoutmos @_StevenNunez @redrapids, @sm_debenedetto, and special guest @ChrisKeathley.

Summarizing some interesting and controversial opinions today 🧵 0/24

#myelixirstatus #elixirnewbie
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.

2/24
Read 25 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(