↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Macro See, Macro Do

The post traces how Phoenix uses quote/2 to package code as AST and how use expands into a call to __using__/1. It uses a small example to show that the shared import and alias setup can be generated instead of written by hand.

Reading notes
#

  • quote/2 appears in Phoenix code that sets up controllers with shared imports and aliases.
  • A simple Bar.AllTheThings example reproduces that pattern with quote do ... end.
  • use Bar.AllTheThings, :things fails until __using__/1 is defined.
  • __using__/1 calls the named function through apply/3.
  • quote do sum(2, 2) end returns AST as a three-element tuple.
  • import Bar.Math also has an AST form with :import and alias metadata.
  • The same AST can be written directly without quote/2.
  • use Bar.AllTheThings, :things expands to Bar.AllTheThings.__using__(:things).
  • The post concludes that quote converts Elixir code to AST and supports dependency injection through macros.