emilsoman
I recently learned that it’s possible to delegate common implementations of protocols using defdelegate in the implementations, but I’m curious to know if this is possible using a macro that provides overridable functions.
The following code works, but shows a warning (see comment below) :
defprotocol MyProtocol do
def foo(data)
def bar(data)
end
defmodule MyProtocol.Defaults do
defmacro __before_compile__(_env) do
quote do
def bar(data) do
IO.puts "Default implementation of bar, data: #{data}"
end
defoverridable [bar: 1]
end
end
end
defimpl MyProtocol, for: Integer do
@before_compile MyProtocol.Defaults
def foo(data) do
IO.puts "int implementation of foo, data: #{data}"
end
# Is it possible to override `bar` from before_compile without this warning:
#
# warning: this clause cannot match because a previous clause at line 28 always matches
# my_protocol.exs:17
def bar(data) do
IO.puts "int implementation of bar, data: #{data}"
end
end
Do I get this warning because before_compile’s return value is injected at the end of the module? Is there any way to get around this?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
I’m not sure I agree with this approach to begin with, but if you were to go this way I think it makes more sense to do a
__using__instead of a before compile. The the function is injected at the top, and you override it in yourdefimplblock. As it is it’s always at the bottom, which is likely what prevents you from overriding it.emilsoman
@benwilson512
What’s the idiomatic way to do this?
Yes, this works. So I guess the problem with before_compile here is that it’s appended to the end of the module body. It makes sense, then. Thanks!
josevalim
You can explicitly delegate to the function you want to:
You can even use
defdelegateto make it more compact:The question you want to ask yourself is: are you really expecting to write the code above so many times to justify the use of indirection and meta-programming? The answer is likely no.
benwilson512
My thoughts precisely.
Ajwah
Can a case be made to have default implementations be delegated to
Anywhen its implementation is missing?The fact we have
@fallback_to_any truemeans that it is not unreasonable to expect that missing implementations are defaulted toAny; so still explicit. Currently a warning is rendered for missing implementations; so the above fallback would get rid of the warning while clearly communicating that its implementation has been omitted deliberately(which could have several reasons, among which is because the domain does not require its implementation.)Cons to the above suggestion: