VeljkoMaksimovic

VeljkoMaksimovic

Multiple aliases in one line with as:

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}

Most Liked

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

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.

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.

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.

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

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement