defoverridable is quite idiomatic and used quite a bit. It is the use of super that is less so. Rather than going to the implementation of the function in the re-implementation, it usually explicitly implements what it needs/wants.
“super” is like having a function called “does_something”. It is non-expressive, and if it is used by every user of that behaviour then it must also be one-size-fits-all.
The exact same thing can be achieved by providing properly named functions that get called. In the example of an overridable upload function, plugins that wish the default behavior can just call Base.upload directly, with an alias Uploader.Base in the Uploader.base’s __using__ macro so you don’t have to put that explicitly in every module that uses it.
The end result is that instead of all of these anonymous and must-fit-all-sizes (a source of horrible code IME) functions, the plugins call the appropriate / desired functions from Base specifically. Something like this:
defmodule Mixin.Base do
@callback c1() :: any
defmacro __using__(_) do
quote do
alias Mixin.Base
@behaviour Mixin.Base
def f1, do: IO.puts "Heya!"
def f2, do: IO.puts "Hey-ho!"
defoverridable [f1: 0, f2: 0]
end
end
end
defmodule Mixin.Specialization do
defmacro __using__(_) do
quote do
def f1, do: IO.puts "This is special"
end
end
end
defmodule Mixin.Impl do
use Mixin.Base
use Mixin.Specialization
def c1 do
# now we compose what we want to happen here ...
f1()
f2()
end
end
Which gets us:
iex(9)> Mixin.Impl.c1
This is special
Hey-ho!
:ok
Which really isn’t so far off from what you had in mind; and then those overridables could be altered by other modules that are used as seen above. If you wish to control the set of plugins with configuration directives at runtime rather than at compile time such as above, a slightly different approach would be needed for the overrides .. but that would also be possible with a bit of creativity.
So I don’t think you are all that far off in your thinking, I would just caution against using super() and thinking too much in terms of inheritance / hierarchies .. hth






















