fgallaire

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.

Showing Posts 1 to 10

mbuhot

mbuhot

How about defp ?

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

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
mudasobwa

mudasobwa

Creator of Cure

+1 to it quickly turns the target module into the spaghetti consisting of calls to some methods, nobody knows where originated from.

fgallaire

fgallaire OP

@josevalim thanks a lot to answer yourself so quickly again.

I agree defdelegate is done for other stuff… but (a lot of) people (like me) are trying to use it to get around import limitations.

Even if you dislike from module import function as new_name it’s explicit and so you have the name to look for in the documentation/Google.

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.

fgallaire

fgallaire OP

OK.

Which ones ?

josevalim

josevalim

Creator of Elixir

The ones mentioned in this thread. You can use alias (or require), wrap it explicitly with defp, etc. If those are not enough, then please tell us a bit more about the problem.

fgallaire

fgallaire OP

I would like the a shortcut for unquote(Macro.escape(x))

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? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews