Elixir - 调用/调用宏 - UndefinedFunctionError
Elixir - Calling / Invoking Macros - UndefinedFunctionError
为什么在 .exs
文件中使用 Module.macroName
语法调用宏时 Elixir 报告 UndefinedFunctionError
?我似乎只有在我有另一个调用宏的函数并且我调用该函数而不是宏时才能调用宏。
下面的代码演示了这一点:
defmodule Sample do
defmacro doIt(expression) do
quote do
IO.puts unquote(expression)
end
end
def doFunc(e), do: doIt(e)
end
Sample.doFunc "Hello World via Function" # Works fine
Sample.doIt "Hello World from Macro!" # Gives error
输出
Hello World via Function
** (UndefinedFunctionError) undefined function: Sample.doIt/1
Sample.doIt("Hello World from Macro!")
(elixir) lib/code.ex:307: Code.require_file/2
Elixir documentation 示例使用 iex
,而不是在 .exs
文件中调用宏。即使在上面的代码中,如果我们删除对 Sample.doIt
的调用并将其加载到 iex
,然后调用 Sample.doIt
就可以正常工作。
E:\elixir>iex hello.exs
Hello World via Function
Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> require Sample
nil
iex(2)> Sample.doIt "Hello"
Hello
:ok
iex(3)>
如果我在上面的文件中尝试 require Sample
,如下所示
defmodule Sample
... rest of stuff as shown above ...
end
require Sample
Sample.doFunc "Hello World via Function"
Sample.doIt "Hello World from Macro!"
我收到错误
** (CompileError) hello.exs:11: module Sample is not loaded but was defined. This happens because you are trying to use a module in the same context it is defined. Try defining the module outside the context that requires it.
(stdlib) lists.erl:1352: :lists.mapfoldl/3
(stdlib) lists.erl:1353: :lists.mapfoldl/3
像往常一样,在使用模块的宏向编译器指示模块的编译顺序之前,您需要先引入模块:
defmodule Other do
def run do
require Sample
Sample.doFunc "Hello World via Function"
Sample.doIt "Hello World from Macro!"
end
end
Other.run # => Hello World via Function
# Hello World from Macro!
为什么在 .exs
文件中使用 Module.macroName
语法调用宏时 Elixir 报告 UndefinedFunctionError
?我似乎只有在我有另一个调用宏的函数并且我调用该函数而不是宏时才能调用宏。
下面的代码演示了这一点:
defmodule Sample do
defmacro doIt(expression) do
quote do
IO.puts unquote(expression)
end
end
def doFunc(e), do: doIt(e)
end
Sample.doFunc "Hello World via Function" # Works fine
Sample.doIt "Hello World from Macro!" # Gives error
输出
Hello World via Function
** (UndefinedFunctionError) undefined function: Sample.doIt/1
Sample.doIt("Hello World from Macro!")
(elixir) lib/code.ex:307: Code.require_file/2
Elixir documentation 示例使用 iex
,而不是在 .exs
文件中调用宏。即使在上面的代码中,如果我们删除对 Sample.doIt
的调用并将其加载到 iex
,然后调用 Sample.doIt
就可以正常工作。
E:\elixir>iex hello.exs
Hello World via Function
Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> require Sample
nil
iex(2)> Sample.doIt "Hello"
Hello
:ok
iex(3)>
如果我在上面的文件中尝试 require Sample
,如下所示
defmodule Sample
... rest of stuff as shown above ...
end
require Sample
Sample.doFunc "Hello World via Function"
Sample.doIt "Hello World from Macro!"
我收到错误
** (CompileError) hello.exs:11: module Sample is not loaded but was defined. This happens because you are trying to use a module in the same context it is defined. Try defining the module outside the context that requires it.
(stdlib) lists.erl:1352: :lists.mapfoldl/3
(stdlib) lists.erl:1353: :lists.mapfoldl/3
像往常一样,在使用模块的宏向编译器指示模块的编译顺序之前,您需要先引入模块:
defmodule Other do
def run do
require Sample
Sample.doFunc "Hello World via Function"
Sample.doIt "Hello World from Macro!"
end
end
Other.run # => Hello World via Function
# Hello World from Macro!