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?
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.
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.
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
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.