Elixir - 模块未使用docs编译

时间:2017-02-28 08:59:02

标签: documentation elixir iex

我昨天刚刚开始学习长生不老药。我有一个文件User.exs。它看起来像这样:

defmodule User do
    @moduledoc """ 
    Defines the user struct and functions to handle users.
    """
    # functions and stuff go here...

end

当我运行iex时,当我尝试查看文档时会发生这种情况:

iex(1)> c "user.exs"
[User]
iex(2)> h User
User was not compiled with docs

有什么想法吗?

2 个答案:

答案 0 :(得分:6)

原因是*.exs文件用于编写脚本,它们不会被编译,而*.ex文件将由elixir编译。

如果您没有混合项目和user.ex文件,请尝试elixirc user.ex,然后启动iex并输入h User

如果你有一个混合项目,那么从命令行启动iex:iex -S mix 这将加载您的项目并编译所有*.ex个文件。现在输入h User

我自己尝试了两种方式并且都工作。

另见:

答案 1 :(得分:6)

c("user.exs")将文件编译到内存中,并且不会将字节码(.beam文件)写入磁盘,而h/1当前需要(详细信息如下)磁盘文件存在于磁盘上才能工作。您可以c将生成的字节码存储在当前目录中,这将使h/1c("user.exs", ".")一起使用:

$ ls
user.exs
$ cat user.exs
defmodule User do
  @moduledoc """
  Defines the user struct and functions to handle users.
  """
end
$ iex
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("user.exs", ".")
[User]
iex(2)> h User

                                      User

Defines the user struct and functions to handle users.

iex(3)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
^C
$ ls
Elixir.User.beam user.exs

h/1依靠Code.get_docs/2获取在模块上调用:code.get_object_code/1的文档。 :code.get_object_code/1根据its docs,"搜索模块模块的目标代码的代码路径。如果成功则返回{Module, Binary, Filename},否则返回error。"

相关问题