Skip to content

Instantly share code, notes, and snippets.

@vasuadari
Last active June 17, 2020 10:05
Show Gist options
  • Save vasuadari/2de4bb554def00fb96a8351fbe66ee84 to your computer and use it in GitHub Desktop.
Save vasuadari/2de4bb554def00fb96a8351fbe66ee84 to your computer and use it in GitHub Desktop.
defoverridable example in elixir
defmodule DefaultModule do
@callback hello(any()) :: any()
defmacro __using__(_opts) do
quote do
@behaviour DefaultModule
def hello(:a) do
:a
end
defoverridable DefaultModule
end
end
end
defmodule Test do
use DefaultModule
def hello(:test) do
:test
end
end
IO.puts Test.hello(:test)
Test.hello(:a)
### Output
# mix run defoverridable.exs
# test
# ** (FunctionClauseError) no function clause matching in Test.hello/1
#
# The following arguments were given to Test.hello/1:
# # 1
# :a
# defoverridable.exs:20: Test.hello/1
# (elixir) lib/code.ex:677: Code.require_file/2
# (mix) lib/mix/tasks/run.ex:136: Mix.Tasks.Run.run/5
# (mix) lib/mix/tasks/run.ex:76: Mix.Tasks.Run.run/1
# (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment