Is defdelegate necessary?

This is a philosophical question, but is defdelegate necessary? Why have a separate keyword to define something that could be accomplished via a normal def with about the same amount of typing?

Isn’t this:

defdelegate reverse(list), to: Enum

The same as this:

def reverse(list), do: Enum.reverse(list)

Or am I missing something?

4 Likes

Not only, it also adds @doc metadata to delegated function.

Generally it’s helper function which simplifies working with code. For sure it standalone does not have lots of benefits, but whole Elixir is written in way which allows you to write only code for your problem and not handle everything around, so in Elixir if you have need for defdelegate then use it.

7 Likes

I don’t think you’re missing anything per se, but I wanted to add that using defdelegate creates a compile-time depdendency between the two modules which is good because it ensures that the module that you’re delegating too exists and has that function defined. And it is also bad since it may increase your compilation time or increase the amount of code that needs to be recompiled when you change a file.

But overall I like the clarity that defdelegate brings. It unambiguously shows that you are delegating the implementation of a function to this other function, and without changing any of the arguments.

19 Likes

Just adding to what @axelson said, I really like the property that it doesn’t change anything. For instance looking at https://github.com/PragTob/benchee/blob/master/lib/benchee.ex

There’s one public run function and then just a bunch of delegates:



    defdelegate init(), to: Benchee.Configuration
    defdelegate init(config), to: Benchee.Configuration
    defdelegate system(suite), to: Benchee.System
    defdelegate benchmark(suite, name, function), to: Benchee.Benchmark
    defdelegate benchmark(suite, name, function, printer), to: Benchee.Benchmark
    defdelegate collect(suite), to: Benchee.Benchmark
    defdelegate collect(suite, printer), to: Benchee.Benchmark
    defdelegate statistics(suite), to: Benchee.Statistics
    defdelegate load(suite), to: Benchee.ScenarioLoader

Makes it very clear that these are there for convenience and even links up the documentation :ok_hand:

11 Likes

Rest answered, that it is meant to provide nicer interface, but if you ask why, then let ask more:

  • is |> necessary? You can always nest calls.
  • is Enum module necessary? You can always use :lists and :maps modules.
  • is in operator necessary? You can always use Enum.member? in body and comparison operators in guards.
  • is if necessary? You can always use case.
  • is with necessary? You can (and in old Elixir version you needed to) use case.

Etc. so as you can see this is just one of the nice sugar things provided by Elixir to simplify work.

16 Likes

Could you please clarify the moment with compile-time dependency? I’ve just checked and it looks like the code

def reverse(list), do: Enum.reverse(list)

creates such dependency as well. How I checked this is I just changed the Enum module to unexisting and the compiler showed the warning about missing module.

You may find this interesting:

5 Likes