Here I am trying to write a macro that generates the dynamic functions.
When I call a function
times_n(3) it should generate the function as
This is how I tried so far
defmodule Times do
defmacro times_n val do
name = "times_" <> to_string(val)
quote bind_quoted: [name: name,val: val] do
n * unquote(val)
defmodule Test do
It is creating function with double quotes wrapping with function name as
"times_4"(n) instead of just
Screen Shot of error
Am I missing anything here?
Thanks in advance
Function names need to be atoms, so after plunbing the string together, you need to convert it to an atom.
Thanks for the quick reply
Can you suggest me where can I read more stuff about the macros in more detail.
I’m in the office right now and our firewall and router are going wild. I can’t reach half of the internet, so I have to make answers sparse.
In general there is the guides about macros on the elixir-lang.org page, this should give you some first idea about how macros work.
Another resource is available in the documentation about the
And of course there is the book “Metaprogramming Elixir” by @chrismccord, published by pragmatic bookstore. I just started reading it, but moving slowly (still in the first chapter after 2 weeks), there is only so little time…