-
-
Save pavlos/c86858290743b4d4fed9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/josevalim/da654648b280f2707f2c