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).
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.
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 import limitations, defdelegate is not one of them.
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.
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.
defmodule Shortcuts do
def sigil_x(quoted, []), do: Macro.escape(quoted)
end
defmodule Test do
import Shortcuts
defmacro me(value) do
quote do
IO.inspect(unquote(~x(value)))
end
end
end
As I’m doing header function pattern matching against it, I need to define it in another module and so import it as @alexiss does and so it’s really verbose and annoying for just that…
That’s not going to work either, you can’t invoke functions inside pattern matching. In this case, if you want to match on an escaped expression, you can use a macro and it will work without imports or separate modules, as long as the macro is defined before you use it:
However, I have to say that matching on escaped expressions is very awkward, and that likely won’t work the way you are expecting.
At this point, it would be better if you gave us the full specification of the problem, instead of giving only bit by bit, which makes it very hard for us to provide precise feedback.
Thanks @josevalim, it’s not awkward, it’s a way to do things, as I’m a beginner I’m testing all the ways to do things in Elixir (and Erlang) in a little project I will release soon.
BTW I still have the undefined function e/1 when matching against a macro defined in the same module.