Oh, we already have a good error message. :)
iex(1)> defmodule Foo do
...(1)> @foo if true do 1 else 2 end
...(1)> end
** (ArgumentError) expected 0 or 1 argument for @foo, got 2.
It seems you are trying to use the do-syntax with @module attributes but
the do-block
is binding to the attribute name. You probably want to wrap the argument
value in
parentheses, like this:
@foo (if true do
1
else
2
end)
(elixir 1.15.0-dev) lib/kernel.ex:3573: Kernel.do_at/5
(elixir 1.15.0-dev) expanding macro: Kernel.@/1
iex:1: (file)
On Wed, Sep 14, 2022 at 12:07 AM José Valim <[email protected]> wrote:
> I missed that.
>
> Changing this requires changing the grammar in a backwards incompatible
> way, which is a no-no.
>
> On Tue, Sep 13, 2022 at 22:57 Austin Ziegler <[email protected]> wrote:
>
>> That’s option 2, and I don’t think it looks readable, which is why I’ve
>> raised this.
>>
>> Like I said, I know *why* this is happening. I’m asking if something more
>> readable can be done.
>>
>> -a
>>
>> On Tue, Sep 13, 2022 at 4:50 PM José Valim <[email protected]> wrote:
>>
>>> Put parens around if and it should work. Otherwise the do/end block
>>> binds to the module attribute. I will make sure we have a good error
>>> message for it.
>>>
>>> On Tue, Sep 13, 2022 at 22:31 Austin Ziegler <[email protected]>
>>> wrote:
>>>
>>>> I’d like to propose a refinement to the way that `@attr` values get
>>>> set. If I want an attribute to have different values at compile-time, I
>>>> have four options (these are *not* ideally formatted):
>>>>
>>>> ```elixir
>>>> @option1 if(Mix.env() == :test, do: "Option 1")
>>>> @option2 (if Mix.env() == :test do
>>>> "Option 2"
>>>> end)
>>>>
>>>> option3 = if Mix.env() == :test do
>>>> "Option 3"
>>>> end
>>>> @option3 option3
>>>>
>>>> if Mix.env() == :test do
>>>> @option4 "Option 4"
>>>> else
>>>> @option4 nil
>>>> end
>>>> ```
>>>>
>>>> These are all, in my opinion, obfuscated.g
>>>>
>>>> What I *want* to do is:
>>>>
>>>> ```elixir
>>>> @option5 if Mix.env() == :test do
>>>> "Option 5"
>>>> end
>>>> ```
>>>>
>>>> But that doesn’t work because of the way that the `@` special form
>>>> works (`@/1`).
>>>>
>>>> Is it possible to either (a) modify the `@` form to accept an
>>>> *expression* instead of expecting a single value, or (b) come up with a
>>>> different way of expressing this? I’m not sure if it’s possible, but maybe
>>>> `@attr = expr`?
>>>>
>>>> I’m not really sure where I’d start exploring it, but it could be
>>>> interesting to explore if this is something that would be considered.
>>>>
>>>> -a
>>>> --
>>>> Austin Ziegler • [email protected] • [email protected]
>>>> http://www.halostatue.ca/ • http://twitter.com/halostatue
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "elixir-lang-core" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQuhJP_vdUXy%3DJUxN5KZLrKYncC1XJPHJBYsopYRiXO%2BZg%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQuhJP_vdUXy%3DJUxN5KZLrKYncC1XJPHJBYsopYRiXO%2BZg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "elixir-lang-core" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BYHOGKky-Tq_ZfdJVKPm1V%3DrXX8UzphKdoohWi4rNNOg%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BYHOGKky-Tq_ZfdJVKPm1V%3DrXX8UzphKdoohWi4rNNOg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Austin Ziegler • [email protected] • [email protected]
>> http://www.halostatue.ca/ • http://twitter.com/halostatue
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "elixir-lang-core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQuWjCtUMWd9KEsAUNA2fBbBLWfhKPaP%3D6j59wrqTK6XOA%40mail.gmail.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQuWjCtUMWd9KEsAUNA2fBbBLWfhKPaP%3D6j59wrqTK6XOA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4LWSQ1RnVGJbv_hjj05cq1zqNLMRYwZsFdkpJjgM2%2BVYA%40mail.gmail.com.