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
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.
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
Enummodule necessary? You can always use:listsand:mapsmodules. - is
inoperator necessary? You can always useEnum.member?in body and comparison operators in guards. - is
ifnecessary? You can always usecase. - is
withnecessary? You can (and in old Elixir version you needed to) usecase.
Etc. so as you can see this is just one of the nice sugar things provided by Elixir to simplify work.
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 ![]()
Last Post!
Eiji
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









