Skip to content

Support Mod.{A, B, C} on imports, alias, require and use #3646

Closed
@josevalim

Description

The goal of this proposal is to support the following syntax:

alias Mod.{A, B, C}
import Mod.{A, B, C}
require Mod.{A, B, C}
use Mod.{A, B, C}

Each call above is equivalent to:

macro Mod.A
macro Mod.B
macro Mod.C

Parser changes

Elixir parser (and possibly tokenizer) will need to be changed in order to support the syntax above. It should not be complicated, as the syntax above was available in the past and then removed. The syntax should emit the AST in a way that:

iex> quote do: Mod.{A, B, C}
{{:., [], [{:__aliases__, [], [:Mod]}, :{}]}, [],
 [{:__aliases__, [], [:A]}, {:__aliases__, [], [:B]}, {:__aliases__, [], [:C]}]}

alias, import, require

When the syntax above is used, Elixir should error if the :as option is given to alias, import or require.

use

use is implemented in pure Elixir which means it will need a separate implementation than the one used for the directives above.

Expansion

The 4 examples above should only work if the the contents inside {} are aliases or atoms. The aliases should never be expanded. For example;

alias Foo, as: A
alias Mod.{A, B, C}

The second will alias Mod.A, Mod.B and Mod.C. It won't alias Mod.Foo since the A inside {...} is never expanded.

Prior art

Both Scala and Rust use a similar syntax as the one defined here.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions