LukasKnuth
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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 useunquoteinside ofquoteit 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
~pthat is pretty similar Using `~p` dynamically inside a macro - #5 by 0xG it may have some useful pointers for you.LukasKnuth
I’m contributing to the Goal library. They use a DSL to generate a schema to validate input against:
This is then implemented as:
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:
This won’t work, because the value of the
:formatoption is a tuple (the quoted string representation of the sigil call).The question now is:
generate_schemafunction needs to handle - and if so, how?