Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Last active August 29, 2015 14:17
Show Gist options
  • Save elbow-jason/baee81b0e2fe8d3f8690 to your computer and use it in GitHub Desktop.
Save elbow-jason/baee81b0e2fe8d3f8690 to your computer and use it in GitHub Desktop.
Dynamic Functions with or without args?
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