The code below is taken from "introducing Elixir" book and it works well. However, if the code in the defmacro is modified such that Enum.map is inside the quote block then the code stops working.
# This code works
defmacro create_functions do
Enum.map planemo_list, fn {name, gravity} ->
quote do
def unquote(:"#{name_drop}")(distance) do
:math.sqrt(2 * unquote(gravity) * distance)
end
end
end
end
end
defmodule Drop do
require FunctionMaker
FunctionMaker.create_functions([{:mercury, 3.7}, {:venus, 8.9},
{:earth, 9.8}, {:moon, 1.6}, {:mars, 3.7}, {:jupiter, 23.1},
{:saturn, 9.0}, {:uranus, 8.7}, {:neptune, 11.0}])
end
The code below does not work once Enum.map is moved inside quote block:
# This code does not work
defmacro create_functions do
quote do
Enum.map unquote(planemo_list), fn {name, gravity} ->
def unquote(:"#{name_drop}")(distance) do
:math.sqrt(2 * unquote(gravity) * distance)
end
end
end
end
What's wrong?
- Thanks