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 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:

12
Post #4
Eiji

Eiji

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.

Eiji

Eiji

You may find this interesting:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement