emilsoman
Overridable functions in protocol implementations?
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?
Most Liked
josevalim
You can explicitly delegate to the function you want to:
defimpl MyProtocol, for: Integer do
def foo(data) do
MyProtocol.Default.foo(data)
end
...
end
You can even use defdelegate to make it more compact:
defimpl MyProtocol, for: Integer do
defdelegate foo(data), to: MyProtocol.Default
...
end
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
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 your defimpl block. As it is it’s always at the bottom, which is likely what prevents you from overriding it.







