Consider the following simple example:
defmodule Test.Foo do
defmacro foo(regex) do
IO.inspect(regex)
end
end
defmodule Test do
require Test.Foo
Test.Foo.foo(~r/test/)
end
{:sigil_r, [delimiter: "/", line: 10], [{:<<>>, [line: 10], ["test"]}, []]}
iex>
Here, the sigil passed to Test.Foo.foo
becomes a quoted expression. When using the macro for code generation, one would simple call unquote/1
on arg
and get the sigil representation again.
However, if I do this in the above example (IO.inspect(unquote(regex))
), I get the following error:
warning: variable “regex” does not exist and is being expanded to “regex()”, please use parentheses to remove the ambiguity or change the variable name
macro_why.exs:3: Test.Foo
** (CompileError) macro_why.exs:3: undefined function regex/0 (there is no such import)
Note that I don’t have a quote
block in my macro. The actual code I’m working on parses the AST directly into an internal representation, so there is no need to quote anything. This is whats kinda throwing me for a loop here.
Hey @LukasKnuth that isn’t how that works. You can think of quote and unquote as similar to ""
and #{}
. You can’t use #{}
outside of a string ""
it just doesn’t make any sense. And when you use unquote
inside of quote
it is not turning it “back into” a sigil, it’s still AST. AST is all there is at the macro level. It’s just interpolating that ast inside of other AST.
Can you show an example of the code you’re trying to get to work in general? Tangentially there is another thread about the use of ~p
that is pretty similar Using `~p` dynamically inside a macro - #5 by 0xG it may have some useful pointers for you.
3 Likes
I’m contributing to the Goal library. They use a DSL to generate a schema to validate input against:
defparams :sigil do
required :id, :string, format: ~r/testing/
end
This is then implemented as:
defmacro defparams(do: block) do
quote do
def schema do
unquote(block |> generate_schema() |> Macro.escape())
end
end
end
defp generate_schema({:__block__, _lines, contents}), do: ...
defp generate_schema({:required, _lines, [field, type, options]}), do: ...
The problem is that the regex becomes the quoted string as above. Then later when I try to do the actual validation based on the schema created from the AST, I try to pattern match on the Regex:
{:format, %Regex{} = regex}, acc ->
validate_format(acc, field, regex)
This won’t work, because the value of the :format
option is a tuple (the quoted string representation of the sigil call).
The question now is:
- Should I fix this call-site (e.g. in the DSL) - and if so, how?
- Or is this something the
generate_schema
function needs to handle - and if so, how?