augnustin

augnustin

Hi,

I have been coding with Elixir for 9+ months and I encountered several time the concept of quotes and each time, I think “ok, today I shall be mature enough to understand quotes”, re-read the content I can find on the internet (starting with the getting started).

But until now, I still haven’t understood the blood out of it. :frowning:

My main question is why do we need them?

If quoting is only about changing the representation of a piece of code, then why should it be so different quoted than unquoted. I’m comfortable with the concept of AST, but as for me it only changes the representation of something, not its content.

Eg. 1 + (2 + 3) <==> add(1, add(2, 3))

Besides, it feels illogical to me that there are two worlds: the quoted one and the unquoted. If quoting is about meta-programming, then quoting quoted expressions should make sense to program meta-meta-programming, shouldn’t it?

Someone in this forum suggested that macros (that are closely related to quotes, aren’t they?) should be seen as functions that return other functions instead of expressions. This helped me a little to read some quote-using-code, but it still doesn’t explain why we need them.

I am comfortable with currying, partial application, function as references, which are the way meta-programming goes in languages I’m more comfortable with (JS, Haskell …), so what’s different here?

Finally, are quotes so hard to understand or is it just me?

Of course, I feel like this is key concept to be comfortable with Elixir, I can be patient with that, just like I was (am?) with Haskell’s monads, but nothing out there suggests that this one has to expect something hard so it may be just me…

Thanks for the help, hope this can be useful to others!

Showing Posts 1 to 4

NobbZ

NobbZ

Quote and unquote are the AST equivalent of the double quote that encloses a string and #{} which you use in a string to inject a variables value rather than just it’s name.

They are essential for code generation, meta programming and macros

entone

entone

Let’s assume we have a function that is going to generate a function for us, and we want to pass in some arguments that can get pattern matched on.

def create_handle_event_function(event, next) do
  quote do
    def handle_event(unquote(event) = runtime_event, state) do
      state = handle_our_event(runtime_event, state)
      {:ok, state, unquote(next)}
    end
  end
end

This allows us to pass in compile time arguments, that can get evaluated/pattern matched at runtime.

So unquote allows us to pass in compile-time args, and get the AST of the value for runtime.

Not sure if that helps at all.

LostKobrakai

LostKobrakai

There are two worlds because there are two times when code is executed:

  • At compile time (any code, which is not a function body is executed + macros)
  • At runtime (Function bodies are executed)

Take this example:

defmodule A do
  for {i, level} <- %{1 => :err, 2 => :warn, 3 => :info} do
    def log_level(i), do: level
  end
end

For the function log_level what do you expect i and level to represent?
Basically it’s 3 times this: def log_level(i), do: level, and it would complain about unmatchable clauses (all match i) as well as crash at runtime for an undefined variable level.

This is where unquoting comes in.

defmodule A do
  for {i, level} <- %{1 => :err, 2 => :warn, 3 => :info} do
    def log_level(unquote(i)), do: unquote(level)
  end
end

At compile time this will define a module A with 3 function heads looking like that:

def log_level(1), do: :err
def log_level(2), do: :warn
def log_level(3), do: :info

The runtime will never know of i and level and therefore not fail because of undefined variables. The same thing happens in macros, where you also create AST at compile time. Macros can however also edit AST.

You might be wondering why this example doesn’t use a quote like macros do. For a module it’s clear which code is compile time executed and which isn’t (only function bodies aren’t executed at compile time), so all the quote/unquote stuff happens as part of the defmodule implementation. For macros there needs to be a distinction between code executed at compile time and the code it actually generates. To not have you manually edit AST nodes there’s quote. It takes its body and converts the contents into AST. It’s basically a AST generator. quote has the same problem as I explained above for the module body, therefore it uses unquote as well, to put data you have at compile time “hard coded” into the generated AST.

The complexity really hits if you want to create the module body from my example (those unquotes are usually named unquote fragments) within the quote of a macro. This is where you need to use the bind_quoted option for quote to remove the ambiguity.

43
Post #3
augnustin

augnustin OP

This is amazing. 1 year later I come back here and now it feels like macros are something I could get comfortable with. :fireworks:

Thanks @LostKobrakai your example really helps.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews