dli

dli

Custom macro doesn't interpolate correctly

I am trying to implement a macro that makes multi-column comparisons less verbose.

# Draft -- does not work
defmacro equals_parent(parent_name, fields) do
  fields
  |> Enum.map(fn field ->
    {field, quote(do: Ecto.Query.parent_as(unquote(parent_name)), unquote(field))}
  end)
end

# Expands to
[
  bar: parent_as(:foo).bar,
  baz: parent_as(:foo).baz
]

# Usage
from c in Child,
  join: p in Parent, as: :foo,
  where: equals_parent(:foo, [:bar, :baz])

However, I am getting this error: (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size

IIUC, I shouldn’t need the ^ caret / interpolation operator because the macro outputs the keyword list at compile time.

I would appreciate if someone helped me understand what’s going on here.

Marked As Solved

dli

dli

Thanks for the pointer! Working solution:

defmacro equals_parent(parent_alias, fields) do
  fields
  |> Enum.map(fn field ->
    quote(do: unquote(field) == parent_as(unquote(parent_alias)).unquote(field))
  end)
  |> Enum.reduce(&quote(do: unquote(&1) and unquote(&2)))
end

iex> from(a in Foo, where: equals_parent(:parent, [:a, :b, :c]))
#Ecto.Query<from a0 in Foo, where: :c == parent_as(:parent).c and (:b == parent_as(:parent).b and :a == parent_as(:parent).a)>

Also Liked

josevalim

josevalim

Creator of Elixir

Sorry, my previous comment was not very clear.

What I mean to say is that, once you have a query expression at the root, then Ecto assumes you are not using keyword lists. Otherwise someone may try to write this:

where: [bar: equals_parent(...)]

And it won’t work.

I think this should fix, but I haven’t tried it:

defmacro equals_parent(parent_name, fields) do
  fields
  |> Enum.map(fn field ->
    {field, quote(do: Ecto.Query.parent_as(unquote(parent_name)), unquote(field))}
  end)
  |> Enum.reduce(quote(do: unquote(&1) and unquote(&2))
end

And it has the benefit that it can now compose with other expressions: where: equals_parent(…) or x.y == z.

josevalim

josevalim

Creator of Elixir

IIUC, I shouldn’t need the ^ caret / interpolation operator because the macro outputs the keyword list at compile time.

I believe the keyword list only works outside of macros. You can’t use it with query expressions. Once you have query expressions, then you need to write or and and, no keyword lists.

I am unsure why would you think this even compiles.

Given they are asking why it doesn’t work, that is not helpful feedback. :slight_smile: Expecting things to work when they don’t is part of coding (and vice-versa)!

LostKobrakai

LostKobrakai

With macros generally you want to complare what the code you want to produce looks like in AST form and then make sure your macro returns the same AST.

You can get the AST from

quote do 
  [code]
end

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement