Using base module function as fallback


SomeModule.action(:ok) # prints "ok passed"
SomeModule.action(:error) # throws error: ** (FunctionClauseError) no function clause matching in SomeModule.inner_handler/1

SomeModule.action(:error) # Should print "base fallback"

Is it possible to use Base.inner_handler as fallback if none of SomeModule.inner_handler definitions does not patternmatch?

defmodule Base do
  defmacro __using__(_) do
    quote do
      import unquote(__MODULE__)

      def action(param), do:

      defp inner_handler(param), do:
        IO.inspect "base fallback"

      defoverridable [inner_handler: 1]

defmodule SomeModule do
  use Base

  defp inner_handler(:ok), do:
    IO.inspect "ok passed"

When an overridable function is overridden, this does not mean ‘apply the new pattern matches at the front and otherwise fall back’. It means ‘replace all function clauses with these new ones’. To still use the old clauses, you should add to SomeModul a function clause

def inner_handler(arg), do: super(arg)