[Proposal] dual alias of the same module should result in a warning

30 views
Skip to first unread message

ku...@goyman.com

unread,
Feb 3, 2022, 3:48:29 PM2/3/22
to elixir-lang-core
If a module is aliased or imported multipled times by the module, it would be handy to have a warning.

For example:

defmodule Foo do

  alias Ecto.Changeset
  alias Ecto.Changeset

end

should result in a warning "duplicate import".

This is low priority, but as there is one for unused import/alias it would help make the code cleaner.

José Valim

unread,
Feb 3, 2022, 5:56:02 PM2/3/22
to elixir-lang-core
We can warn it for aliases no problems and a PR would be welcome.

However, for imports it is a bit more complicated because you can import something multiple times with different parts. Furthermore, maybe a library Bar is doing "import Foo" and you want to repeat "import Foo" for clarity. I think we will have false positives.

--
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/0d85ffa7-3997-4637-915e-523a744d155cn%40googlegroups.com.

ku...@goyman.com

unread,
Feb 3, 2022, 5:57:56 PM2/3/22
to elixir-lang-core
Yeah I agree that import is more tricky. But for me, alias is the main use case. I'll see if I can manage a PR.

Jon Rowe

unread,
Feb 4, 2022, 2:49:36 AM2/4/22
to Damir
One of the same problems with `import` applies to `alias` too though, if a library has `alias Module` in its `use` macro it would cause this warning if you aliased it yourself to be explicit. Prehaps this is more suited to a static analysis tool like credo which already warns you if your aliases and import declarations are out of order, it could warn you if you have them twice in the same file without hassling those who just like to be explicit in files.

Just my 2¢

Cheers
Jon

Aleksei Matiushkin

unread,
Feb 4, 2022, 3:57:03 AM2/4/22
to elixir-l...@googlegroups.com
Another issue with an alias would be with many different aliases of the same module. E. g. in Ecto if the schema has two distinct associations with the same foreign schema, one might want to improve code readability by

alias User, as: Customer # for customer-aware queries
alias User, as: Seller # for seller-aware queries





--
Aleksei MatiushkinSoftware Engineer - R&D
 
 


8 Devonshire Square, London, EC2M 4PL, United Kingdom
Torre Mapfre, Planta 22, Marina, 16-18, 08005 Barcelona, Spain

  








LinkedIn    Twitter    YouTube
 
Kantox Limited is a UK private company with registered company number 07657495 and registered address at 8 Devonshire Square, London EC2M 4PL, United Kingdom. We are authorised with the UK Financial Conduct Authority (FCA) under the Payment Service Regulation 2017 as a Payments Institution (FRN 580343) for the provision of payment services and with HMRC as a Money Service Business Registration No.12641987.
Kantox European Union, S.L.  is a Spanish private company with tax ID number B67369371 and registered address at Torre Mapfre, Planta 22, Marina, 16-18, 08005 Barcelona, Spain. Kantox is authorized by the Bank of Spain, with registration number 6890, which is the supervisor of the Spanish banking system along with the European Central Bank. Additionally, we are supervised by SEPBLAC, the Supervisory Authority for the prevention of money laundering and terrorist financing in Spain.
KANTOX is the Controller for the processing of data in accordance with the GDPR and LOPDGDD for the purpose of maintaining a commercial relationship. You may exercise your rights of access and rectification, portability, restriction and opposition by writing to KANTOX to the email: gd...@kantox.com. You have your right to make a complaint at www.aepd.es.  

José Valim

unread,
Feb 4, 2022, 3:59:37 AM2/4/22
to elixir-lang-core
Said case would not warn. I think we should warn only if you are defining an alias, and it already exists, and it points to the same module.

Elixir also knows if an alias was defined by a macro, so we can skip the warning in such cases too.

Reply all
Reply to author
Forward
0 new messages