I have an is_nil guard against a module attribute compiled with Application.get_env. Something like the code below.
defmodule Foo do
@secret Application.get_env(:foo, :secret)
# Always return :ok if secret isn't set (i.e. in development)
def check_secret(_) when is_nil(@secret), do: :ok
def check_secret(secret) when secret == @secret, do: :ok
def secret(_), do: :error
end
This gives me a warning though: warning: this check/guard will always yield the same result. Can I suppress this warning? Or should I solve this differently?
I ‘think’ this would work (but untested of course):
defmodule Foo do
secret = Application.get_env(:foo, :secret)
# Always return :ok if secret isn't set (i.e. in development)
def check_secret(_) when unquote(quote(generated: true, do: is_nil(unquote(secret))), do: :ok
def check_secret(secret) when unquote(quote(generated: true, do: secret == unquote(secret)), do: :ok
def secret(_), do: :error
end
Which of course you could wrap up in a macro if used often (or to make it more readable). You ‘might’ need to mark the def itself as generated, but a macro could do that easily too (or pipe it through, just be careful not to mark the body as generated to prevent hiding issues inside).
But @NobbZ way is the best for generic calls and readability, don’t use the above unless you are optimizing for absolute efficiency at the cost of readability.
Do you have a special reason to use that module attribute? Some name it an anti-pattern, discussion on using (secret) config info in this way and alternatives you can find here: