Kernel.defoverridable
You're seeing just the macro
defoverridable
, go back to Kernel module for more information.
Makes the given functions in the current module overridable.
An overridable function is lazily defined, allowing a developer to override it.
Macros cannot be overridden as functions and vice-versa.
Example
defmodule DefaultMod do
defmacro __using__(_opts) do
quote do
def test(x, y) do
x + y
end
defoverridable test: 2
end
end
end
defmodule InheritMod do
use DefaultMod
def test(x, y) do
x * y + super(x, y)
end
end
As seen as in the example above, super
can be used to call the default
implementation.
If @behaviour
has been defined, defoverridable
can also be called with a
module as an argument. All implemented callbacks from the behaviour above the
call to defoverridable
will be marked as overridable.
Example
defmodule Behaviour do
@callback foo :: any
end
defmodule DefaultMod do
defmacro __using__(_opts) do
quote do
@behaviour Behaviour
def foo do
"Override me"
end
defoverridable Behaviour
end
end
end
defmodule InheritMod do
use DefaultMod
def foo do
"Overridden"
end
end