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?

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 #3
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 #5
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 #4

Last Post!

Eiji

Eiji

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dokuzbir
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement