【问题标题】:How can I load a module into another module using Elixir language?如何使用 Elixir 语言将一个模块加载到另一个模块中?
【发布时间】:2015-12-06 15:18:23
【问题描述】:

我很难使用 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

【问题讨论】:

  • 同一目录下有2个文件。 a.ex 和 b.ex

标签: elixir


【解决方案1】:

在您的文件 b.ex 中删除最后一行的 B.b

然后在你的项目目录中像这样运行Iex

iex -S mix

这将加载 iex 并正确加载您的模块

那你就可以B.b

你会看到:

B.b
A.a
:ok

另外,请确保您的 a.ex 和 b.ex 文件位于您的 elixir 项目的 lib/ 目录中

【讨论】:

  • @HideshiOgoshi 如果有效,您应该接受答案
  • 我尝试运行相同的代码但无法运行。我在命令提示符“G:\work\Project1>iex -S mix”中运行了“iex -S mix”,其中我在 G:\work\Project1 中有 lib\ProjectName 文件夹。我得到以下 iex(1)> B.b ** (UndefinedFunctionError) function B.b/0 is undefined (module B is not available) B.b()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2022-10-09
  • 1970-01-01
相关资源
最近更新 更多