Skip to content

Enable use of unquote in defguard(p) #13716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged

Conversation

alisinabh
Copy link
Contributor

Currently, it is not possible to define a guard in Elixir using a dynamic name at compile time like it it possible with def and defmacro.

As an example the following code fails to compile

defmodule MyModule do
  @states [:a, :b, :c]

  for state <- @states do
    defguard unquote(String.to_atom("is_#{state}"))(state) when state == unquote(state)
  end
end
== Compilation error in file text.ex ==
** (ArgumentError) invalid syntax in defguard unquote(String.to_atom("is_#{state}"))(state)
    (elixir 1.17.0) lib/kernel.ex:5789: Kernel.define_guard/3
    (elixir 1.17.0) expanding macro: Kernel.defguard/1
    text.ex:5: MyModule (module)

This ensures that an unquote syntax is also accepted in defguard and defguardp + tests.

Currently, it is not possible to define a guard in Elixir using a
dynamic name at compile time like it it possible with `def` and `defmacro`.

This ensures that an unquote syntax is also accepted in `defguard` and
`defguardp`.
@josevalim josevalim merged commit 7a90b59 into elixir-lang:main Jul 10, 2024
7 of 9 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants