Nope, all you need is something like this:
defmodule ParseBehavior do
# Obviously replace any with your data types
@callback parse(data :: any) :: any
end
defmodule CustomerA do
@behaviour ParseBehavior
end
If you don’t implement the specified callbacks you will get a compilation warning.
You can also have the variant with the default implementation:
defmodule ParseBehavior do
@callback parse(data :: any) :: any
defmacro __using__(_opts) do
quote do
@behaviour ParseBehavior
def parse(data) do
# This will be the default implementation if not overriden
end
defoverridable parse: 1
end
end
end
defmodule CustomerA do
use ParseBehavior
# This definition will override the default definition
def parse(data) do
end
end
I wouldn’t recommend using the variant with default implementation unless that is strictly required, as it makes the code harder to read.






















