fgallaire
ISTM that alias, import and defdelegate are limited and confusing, and certainly my worst experience in Elixir.
I just would like to do the Elixir equivalent of Python from module import function as new_name for coding concision and clarity.
alias works only for module names
import has except and only keywords but not an as or alias one
defdelegate:
- doesn’t work with macros
- has a logical but confusingly inversed
as:keyword - use confusing “shadow variables” (instead of arity as in
import) - needs to be done in a second module to be call in the first one “body”
To avoid the defdelegate mess which is not done for that, we should have a working import with alias mecanism.
Trending in Questions
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mbuhot
How about
defp?LostKobrakai
How would renaming functions be clearer than using the
OtherModule.functioniffunctionis also defined in the local module? If you rename functions on a per module basis I’d rather expect that to become a hot mess very soon. I’d argue that if you have conflicting imports opting forrequire Moduleorrequire MyApp.Super.Long.Module, as: Moduleis the better solution (oraliasinstead ofrequireif it’s not about macros).josevalim
aliasandimportserve complete different purposes thandefdelegate.defdelegateis about extending your public API by delegating to something else, that’s why it has different requirements.I really dislike
from module import function as new_nameseen in Python and in other languages because you are introducing a name in that project that can’t be found anywhere else. If you rename something toprefix_foo_bar, that name exists only in that file, and trying to look for it in the documentation, Google, or anywhere else will most likely fail. If you really want to rename something, it is better to be explicit at it and define a private function you use locally. Or, as others have said, rely on aliases to do the disambiguation.mudasobwa
+1to it quickly turns the target module into the spaghetti consisting of calls to some methods, nobody knows where originated from.fgallaire
@josevalim thanks a lot to answer yourself so quickly again.
I agree
defdelegateis done for other stuff… but (a lot of) people (like me) are trying to use it to get aroundimportlimitations.Even if you dislike
from module import function as new_nameit’s explicit and so you have the name to look for in the documentation/Google.josevalim
We can’t evaluate language features in a vacuum.
In Python, I don’t think you can meta-program the construct above, so it is always explicit. In Elixir, however, that could be hidden inside a
use, and that would be a recipe for disaster.There are other ways to work around
importlimitations,defdelegateis not one of them.fgallaire
OK.
Which ones ?
josevalim
The ones mentioned in this thread. You can use
alias(orrequire), wrap it explicitly withdefp, etc. If those are not enough, then please tell us a bit more about the problem.fgallaire
I would like the a shortcut for
unquote(Macro.escape(x))josevalim
You can’t define a shortcut for that. This is not related to
import,aliasordefdelegate.unquotemakes sense only inside a quoted expression because it is a way to tellquotethat it should not touch that particular piece of code.To make a comparison, what you are asking is to move the
#{inspect(foo)}from"a string: #{inspect(foo)}"to a separate function. But#{...}only makes sense inside a string.EDIT: What you can do, if your macros are verbose, is to escape before the quote, instead of defining all of the code inline. Instead of:
You can do:
But keep in mind
quoteandunquotein Elixir are verbose on purpose (especially compared to Lisps which often have one or two special characters for those) because we want you to be careful with your macro, quoting and unquoting usage.