Athunnea

Athunnea

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

Showing Posts 1 to 4

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.

KristerV

KristerV

necrobump time. here’s my ready made script that supports different arities and a custom list of modules to combine into one.

defmodule MyApp.FullHouse do
  use MyApp.FullHouse.Populate,
    context_modules: [
      MyApp.ModuleA,
      MyApp.ModuleB,
      MyApp.ModuleC,
      MyApp.ModuleD
    ]
end

defmodule MyApp.FullHouse.Populate do
  require Logger

  defmacro __using__(opts) do
    modules =
      for {_, _, module_path} <- opts[:context_modules] do
        ("Elixir." <> Enum.join(module_path, "."))
        |> String.to_atom()
      end

    for module <- modules, {fun, arity} <- apply(module, :__info__, [:functions]) do
      quote do
        defdelegate unquote(fun)(unquote_splicing(Macro.generate_arguments(arity, module))),
          to: unquote(module)
      end
    end
    |> tap(fn x -> Logger.info("Service Layer has #{length(x)} functions.") end)
  end
end

Dunno if this is efficient or not, I’m just using this for prototyping to think my way to a better process. Pretty bad code smell if this ends up in production.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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

nelthaarion
I’ve been working on Breeze, a high-performance web framework for Go. The main idea is pretty simple: keep the hot path small, avoid unn...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews