Proposal: @attr = expr for improved readability of conditional values

41 views
Skip to first unread message

Austin Ziegler

unread,
Sep 13, 2022, 4:31:30 PM9/13/22
to elixir-l...@googlegroups.com
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

if Mix.env() == :test do
  @option4 "Option 4"
else
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
--

José Valim

unread,
Sep 13, 2022, 4:50:19 PM9/13/22
to elixir-l...@googlegroups.com
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.

--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQuhJP_vdUXy%3DJUxN5KZLrKYncC1XJPHJBYsopYRiXO%2BZg%40mail.gmail.com.

Adam Lancaster

unread,
Sep 13, 2022, 4:54:27 PM9/13/22
to elixir-l...@googlegroups.com
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



Austin Ziegler

unread,
Sep 13, 2022, 4:57:10 PM9/13/22
to elixir-l...@googlegroups.com
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

Austin Ziegler

unread,
Sep 13, 2022, 5:04:03 PM9/13/22
to elixir-l...@googlegroups.com
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.)


José Valim

unread,
Sep 13, 2022, 6:07:59 PM9/13/22
to elixir-l...@googlegroups.com
I missed that.

Changing this requires changing the grammar in a backwards incompatible way, which is a no-no.

José Valim

unread,
Sep 13, 2022, 6:14:00 PM9/13/22
to elixir-lang-core
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)

Reply all
Reply to author
Forward
0 new messages