Last active
March 25, 2018 15:18
-
-
Save sofakingworld/352545b6623cd94e212085578ad0b335 to your computer and use it in GitHub Desktop.
Meta Elixir (Create methods dynamic)
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 Dynamic do | |
@methods ~w(method1 method2 method3)a | |
@methods | |
|> Enum.each( fn method -> | |
method_name = "prefix_#{method}" | |
def unquote(:"#{method_name}")(argument1, argument2) do | |
{unquote(method), argument1, argument2} | |
end | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iex> Dynamic.prefix_method1 1, 3
=> {:method1, 1, 3}
iex> Dynamic.prefix_method2 :_some_atom, [1, 2, 3]
=> {:method2, :some_atom, [1, 2, 3]}
iex> Dynamic.prefix_method3 nil, nil
=> {:method3, nil, nil}