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
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
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.
2
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.
2
Popular in Questions
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









