Not the same behaviour at all, although `Application.compile_env/2` would
probably do what I want at the expense of adding a test config.

The examples that I gave are quite similar to the code we’re running. We’re
using an umbrella where we have a main "web" app and then a bunch of other
"web" apps (called something else, of course). For test purposes, an
endpoint must be defined and should be started in `start/2` callback.
Therefore, we’re doing:

```elixir
@endpoint if(Mix.env() == :test, do: MySubAppWeb.Endpoint)
```

(We’ve actually switched to option 3 in my original message, but the point
is the same.)

We literally want this to be `nil` every time we are running this except
under test. (We’re also filtering the `children` for
`Supervisor.start_link/2`, so `nil` isn’t an issue.)


On Tue, Sep 13, 2022 at 4:54 PM Adam Lancaster <[email protected]> wrote:

> Would it not be preferable to:
>
> @my_attr Application.compile_env!(:my_app, :my_key)
>
> And set a different value for the attr in the different config envs?
>
> Best
>
> Adam
>
>
>
> On Tue, 13 Sep 2022 at 21:50, 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>
>> .
>>
> --
> 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/CADwgC_BEocb49QVs23Njdr8VVfZeVTzDuYN7Xm8Tx%3DmzAd9puA%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CADwgC_BEocb49QVs23Njdr8VVfZeVTzDuYN7Xm8Tx%3DmzAd9puA%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/CAJ4ekQsLj7Ru%3DeW682zAVkp_VuaV6g6mZcMu-a9%3DmgA7qBPeMg%40mail.gmail.com.

Reply via email to