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.

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 #4
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
LostKobrakai

LostKobrakai

How would renaming functions be clearer than using the OtherModule.function if function is 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 for require Module or require MyApp.Super.Long.Module, as: Module is the better solution (or alias instead of require if it’s not about macros).

josevalim

josevalim

Creator of Elixir

You can’t define a shortcut for that. This is not related to import, alias or defdelegate. unquote makes sense only inside a quoted expression because it is a way to tell quote that 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:

quote do
  ...unquote(Macro.escape(x))...
end

You can do:

x = Macro.escape(x)
quote do
  ...unquote(x)...
end

But keep in mind quote and unquote in 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.

Where Next?

Popular in Questions Top

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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics 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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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