我很难使用 Elixir 语言将一个模块加载到另一个模块中。
例如,我有 2 个文件如下所示:
a.ex
defmodule A do
def a do
IO.puts "A.a"
end
end
b.ex
defmodule B do
require A
def b do
IO.puts "B.b"
A.a
end
end
B.b
我试图执行 b.ex。然后我得到如下所示的错误:
$elixir b.ex
** (CompileError) b.ex:2: module A is not loaded and could not be found
最佳答案
在您的文件中b.ex
从最后一行删除 B.b
然后在你的项目目录中像这样运行 Iex
iex -S mix
这将加载 iex 并正确加载您的模块
然后你可以做
B.b
你会看到:
B.b
A.a
:ok
另外,请确保您的
a.ex
和 b.ex
文件位于 lib/
您的 elixir 项目目录
关于elixir - 如何使用 Elixir 语言将一个模块加载到另一个模块中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113522/