Skip to content

Instantly share code, notes, and snippets.

@nickretallack
Created September 5, 2014 21:51
Show Gist options
  • Save nickretallack/67dd0988c4d784de9258 to your computer and use it in GitHub Desktop.
Save nickretallack/67dd0988c4d784de9258 to your computer and use it in GitHub Desktop.
defmodule Foo do
defmacro __using__(_) do
quote do
def foo() do
IO.puts "Foo"
end
defoverridable [foo: 0]
end
end
end
defmodule Bar do
defmacro __using__(_) do
quote do
defmacro try_super(args) do
if Module.overridable?(__CALLER__.module, __CALLER__.function) do
quote do: {:ok, super(unquote_splicing(args))}
else
:error
end
end
def foo() do
IO.puts "Baz start"
try_super([])
IO.puts "Baz end"
end
defoverridable [foo: 0]
end
end
end
defmodule Test1 do
use Foo
use Bar
end
IO.puts "\nTest with a super"
Test1.foo
# Baz start
# Foo
# Baz end
defmodule Test2 do
use Bar
end
IO.puts "\nTest without a super"
Test2.foo
# Baz start
# Baz end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment