fgallaire

fgallaire

Alias, import and defdelegate are a mess?

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.

First Post!

mbuhot

mbuhot

How about defp ?

Most Liked

josevalim

josevalim

Creator of Elixir

alias and import serve complete different purposes than defdelegate. defdelegate is 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_name seen 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 to prefix_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.

14
Post #3
josevalim

josevalim

Creator of Elixir

We can’t evaluate language features in a vacuum. :slight_smile: 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 import limitations, defdelegate is not one of them.

alexiss

alexiss

The concrete realization of e in comments above depends on your needs, thats because we are asking you about your whole problem. I think, you don’t need Macro.escape at all. For example to match on map keys solution may looks like this:

defmodule Test do
  defmacrop e(kv) do
    quote do
      %{unquote_splicing(kv)}
    end
  end

  def me(e(a: 10)), do: :a_key_10
  def me(e(a: 20)), do: :a_key_20
  def me(e(b: _)),  do: :b_key
end

then in iex:

iex(3)> Test.me(%{a: 10})
:a_key_10
iex(4)> Test.me(%{a: 20})
:a_key_20
iex(5)> Test.me(%{b: :any})
:b_key

Last Post!

NobbZ

NobbZ

Importing is done via the import call. It will enable you to call functions from the imported module in your current scope without having to qualify them by the module.

So after import Enum, you can write map(list, &(&1+1)) instead of having to write Enum.map(list, &(&1+1)).

importing is rarely really necessary, and unless building an EDSL should be avoided.

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
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
JesseHerrick
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
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement