Athunnea

Athunnea

Batch delegate functions to a module?

When you delegate a bunch of functions to the same module, is there a way to batch delegate them?

Instead of

defdelegate a1(), to: ABC
defdelegate a2(), to: ABC
defdelegate a3(), to: ABC

something like

defdelegate to: ABC do
  a1()
  a2()
  a3()
end

Most Liked

kip

kip

ex_cldr Core Team

You can use some simple meta programming like this:

defmodule Delegate do
  @delegate_functions ~w(a b c d e f)a
  
  for fun <- @delegate_functions do
    defdelegate unquote(fun)(), to: OtherModule
  end
end
13
Post #1
dimitarvp

dimitarvp

This is a very valid way but for what it’s worth I’d either add all delegates by hand or make a short sed or awk script do it for me.

Using metaprogramming for such a simple task hurts readability and the ease of future reverse-engineering effort required to maintain the code.

Just adding nuance to your already valid advice.

dimitarvp

dimitarvp

There’s a better way to do this if module_path is a list of strings:

module_path
|> Enum.map(&String.to_atom/1)
|> Module.concat()

If it’s a list of atoms (like it seems to be from your post) then just remove the Enum.map part and use Module.concat directly.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New