Skip to content

Instantly share code, notes, and snippets.

@pavlos
Forked from josevalim/extension.ex
Created March 10, 2016 09:26
Show Gist options
  • Save pavlos/c86858290743b4d4fed9 to your computer and use it in GitHub Desktop.
Save pavlos/c86858290743b4d4fed9 to your computer and use it in GitHub Desktop.
defmodule Extension do
defmacro extends(module) do
module = Macro.expand(module, __CALLER__)
functions = module.__info__(:functions)
signatures = Enum.map functions, fn { name, arity } ->
args = Enum.map 0..arity, fn(i) ->
{ binary_to_atom(<< ?x, ?A + i >>), [], nil }
end
{ name, [], tl(args) }
end
quote do
defdelegate unquote(signatures), to: unquote(module)
defoverridable unquote(functions)
end
end
end
defmodule MyList do
import Extension
extends List
def flatten(_) do
IO.puts "Overriden flatten"
end
end
IO.inspect MyList.delete([1,2,3], 3)
MyList.flatten([1,2,3])
@pavlos
Copy link
Author

pavlos commented Mar 10, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment