fireproofsocks

fireproofsocks

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?

Showing Posts 1 to 6

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.

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

devality

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.

Eiji

Eiji

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews