LukasKnuth

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.

Showing Posts 1 to 2

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

LukasKnuth

LukasKnuth OP

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?
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews