abdulsattar

abdulsattar

How does this work?

query = from u in "users",
          where: u.age > 18,
          select: u.name

I get that from is a macro, which essentially takes two arguments:

query = from(u in "users",
          [where: u.age > 18,
          select: u.name])

which makes it more mysterious. u in "users" what does it do? The keyword list has u.age, where does the u come from? How is throwing u not defined exception?

The macro based syntax is also suprising:

"users"
|> where([u], u.age > 18)
|> select([:name])

How does where([u], u.age > 18) not fail with u not defined?

Showing Posts 1 to 2

LostKobrakai

LostKobrakai

Elixir is not directly using what you wrote for the ecto query, but ecto transforms the AST of your code into something that makes sense to elixir.

minhajuddin

minhajuddin

A macro doesn’t execute the code passed in, It gets an AST of the code and it can then do whatever it wants. I haven’t looked at how Ecto actually does this, I am assuming that it gets a hold of the AST and transforms it into a SQL query based on the operators that are being used and then it executes the code. One way to inspect it is by looking at the struct that was returned:

iex(7)> q = from(u in User, where: u.id == 3, order_by: [asc: :id]) |> Map.from_struct
%{
  assocs: [],
  distinct: nil,
  from: {"users", UA.Accounts.User},
  group_bys: [],
  havings: [],
  joins: [],
  limit: nil,
  lock: nil,
  offset: nil,
  order_bys: [
    %Ecto.Query.QueryExpr{
      expr: [asc: {{:., [], [{:&, [], [0]}, :id]}, [], []}],
      file: "iex",
      line: 7,
      params: []
    }
  ],
  prefix: nil,
  preloads: [],
  select: nil,
  sources: nil,
  updates: [],
  wheres: [
    %Ecto.Query.BooleanExpr{
      expr: {:==, [],
       [
         {{:., [], [{:&, [], [0]}, :id]}, [], []},
         %Ecto.Query.Tagged{tag: nil, type: {0, :id}, value: 3}
       ]},
      file: "iex",
      line: 7,
      op: :and,
      params: []
    }
  ]
}

Here is an example that should give you more clarith around macros, the expression 1 + 2 is never evaluated:

defmodule M do
  defmacro add({:+, _, [op1, op2]}) do
    quote do
      "Add #{unquote(op1)} and #{unquote(op2)} yourself!"
    end
  end
end

defmodule Test do
  def test do
    require M
    M.add(1 + 3)
  end
end

Test.test() |> IO.puts
# => Add 1 and 3 yourself!
— 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
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
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