VeljkoMaksimovic

VeljkoMaksimovic

Is there a way to have multiple aliases, but to use as: acronym for one of them, something like this, but that actually works :smiley:

alias Guard.Rbac.{Cache, RoleBindingIdentification, as: RBI}

Showing Posts 1 to 10

Eiji

Eiji

What you show in best case could work as:

alias Guard.Rbac, as: RBI
alias Guard.Rbac.{Cache, RoleBindingIdentification}

That’s because opts are on same level as other aliases.

Alternatively Elixir could support this:

alias Guard.Rbac.{Cache, {RoleBindingIdentification, as: RBI}}

However this looks just ugly and I guess that’s why it would be not supported at all. If you have an interesting idea how to improve that then feel free to create a proposal, but so far I do not see either a use case nor how it could look good at all.

Also keep in mind that even if such ugly-looking feature would be supported then it would be still formatted into multiple lines, so it would look like:

alias ParentName.{
        {ChildA, as: A},
        ChildB,
        {ChildC, as: C}
      }

I believe that keeping aliases work as it’s now is best.

VeljkoMaksimovic

VeljkoMaksimovic OP

This is exactly what I meant :smiley: I dont agree that this is ‘ugly looking’. Elixir already supports this notation:

alias ParentName.{
    ChildA,
    ChildB,
    ChildC
}

and I don’t see how this is any prettier (or less ‘ugly-looking’) then the example above where abbreviations are supported.

True, situations where something like this would be needed are rare, but option 1:

alias Guard.Repo.{
    RbacUser,
    UserGroupBinding,
    RbacRole,
    {OrgRoleToProjRoleMapping, as: Mapping}
    RoleInheritance,
    SubjectRoleBinding,
    {RoleBindingIdentification, as: RBI}
    Permission,
    Scope
  }

IMO looks cleaner then option 2:

alias Guard.Repo.OrgRoleToProjRoleMapping, as: Mapping
alias Guard.Repo.RoleBindingIdentification, as: RBI
alias Guard.Repo.{
    RbacUser,
    UserGroupBinding,
    RbacRole,
    RoleInheritance,
    SubjectRoleBinding,
    Permission,
    Scope
  }
bmitc

bmitc

I don’t understand what that’s trying to specify. In this case, one is able to guess, though.

For me personally, I enforce the Credo.Check.Readability.AliasAs and Credo.Check.Readability.MultiAlias Credo checks for alias, among others. So the alias ___.{___} syntax and alias ___, as: ___ are not allowed.

Mutli aliasing is really only useful in very specific scenarios. It makes copying module names harder (impossible rather), and it starts to look ugly and busy when you have a lot of modules that aren’t just under a single “namespace”. Simply typing out the shared “namespace” is really not difficult.

I personally feel that alias ___, as: ___ should be used extremely sparingly if not removed from the language. It’s use is usually an indication that the module it is asing is poorly named. And I have certainly seen it heavily abused where the module was essentially renamed with as.

For the proposed:

alias Guard.Repo.{
    RbacUser,
    UserGroupBinding,
    RbacRole,
    {OrgRoleToProjRoleMapping, as: Mapping}
    RoleInheritance,
    SubjectRoleBinding,
    {RoleBindingIdentification, as: RBI}
    Permission,
    Scope
  }

it requires a fair amount of tracing to understand. I would suggest renaming the longer modules. RoleBindingIdentification could be RoleBindingId or even just RBI if you documented it with @moduledoc and the abbreviation is clear in the context. OrgRoleToProjRoleMapping should probably just be renamed.

NobbZ

NobbZ

Using rarely, yes. Removing? No!

We have a codebase with more than 600 modules at work.

There are several modules ending in Product, within Schema, Dataloader, and what not prefixes. We do not want to repeat the type in the module name itself, therefore we are happy to be able to use :as in an alias.

bmitc

bmitc

Removed was a bit of a hyperbole. :stuck_out_tongue_closed_eyes: I know it won’t and probably shouldn’t happen in Elixir. Although, certainly several other languages, such as F#, do not have that feature, and I personally don’t miss it there. I’m not aware of it being in Erlang. Is that correct?

For Elixir, I got a bad taste for it at my previous company, where its use was definitely a symptom of poorly named modules. So many modules had such extremely similar names that as: ___ was used to mitigate this in modules and in tests. The moral of the story is that people will often take conveniences and turn them into hazards. :upside_down_face:

eksperimental

eksperimental

And how do you deal with external librarie modules?

ityonemo

ityonemo

Erlang doesn’t have module aliasing at all.

I’d say the biggest reason why as: is bad because if you wanted to do a global search and replace during refactoring, it makes it much more difficult.

Same with alias Module.{…}

I use neither in my codebases.

sodapopcan

sodapopcan

Ya, I’m not a huge fan of {} (though it has its uses though wouldn’t miss them if they were gone) and never use :as. I even avoid just plain old alias a lot of the time unless it provides significant noise-reduction.

One of the big things that drew me to Elixir was the high level of locality that is encouraged through official examples. I’m even just talking things like, Enum.map as opposed to import Enum, only: [:map, 2]. Jumping into a function to gain a bit of context on its caller and being able to see all its dependencies without having to jump any further is a really nice feeling. Of course, it’s a balance, but I feel that often there is a subconscious emphasis put on writeability or even “prettiness” over readability and, more importantly, “scannability”.

And of course the other thing I really like about Elixir is how relatively small it is and how much thought is put into backwards incompatible changes. Just by that alone, I don’t see something like nested :ases ever making it in (not that you were necessarily suggesting they would be). Take this really well-thought-out proposal for field punning—a featured loved by many in other languages (not me)—that was rejected, for example.

hst337

hst337

Yes, there is!

Behold the dark art of context magic in Elixir

 alias (alias Guard.Rbac, as: RBI).{Cache, RoleBindingIdentification}

Please do not try this at home

VeljkoMaksimovic

VeljkoMaksimovic OP

If I understand correctly, in your example RBI.Cache would ‘expand’ to Guard.Rbac.Cache, and same go for RBI.RoleBindingIdentification which would expand to Guard.Rbac.RoleBindingIdentification, right?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews