fireproofsocks

fireproofsocks

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?

First Post!

Eiji

Eiji

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

https://github.com/elixir-lang/elixir/pull/8228

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.

Most Liked

axelson

axelson

Scenic Core Team

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.

22
Post #2
hauleth

hauleth

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.

18
Post #4
PragTob

PragTob

Just adding to what @axelson said, I really like the property that it doesn’t change anything. For instance looking at benchee/lib/benchee.ex at main · bencheeorg/benchee · GitHub

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:

12
Post #3

Last Post!

Eiji

Eiji

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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 42716 114
New

We're in Beta

About us Mission Statement