Last active
August 29, 2015 14:17
-
-
Save elbow-jason/baee81b0e2fe8d3f8690 to your computer and use it in GitHub Desktop.
Dynamic Functions with or without args?
This file contains hidden or 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 DynaFuncs do | |
#this works - dynamic name with no args | |
{:ok, other_func_name} = Code.string_to_quoted(:half) | |
def unquote(other_func_name), do: IO.puts(1/2) | |
# but this doesn't work - static name with no args | |
def unquote(:one_third), do: IO.puts(1/3) | |
# and this works - static name with args | |
def unquote(:reciprocal)(num), do: IO.puts(1/num) | |
# but this doesn't work - dynamic name with args | |
{:ok, funcname} = Code.string_to_quoted(:multiplicative_inverse) | |
def unquote(funcname)(num), do: IO.puts(1/num) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment