13

I just started learning elixir yesterday. I have a file User.exs. It looks like this:

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

end

When I run iex, this is what happens when I try to see the docs:

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

Any ideas?

3
  • Change the extension of the file to be .ex. Commented Feb 28, 2017 at 9:02
  • @mudasobwa Problem remains, no different. Commented Feb 28, 2017 at 9:30
  • Normally a .exs is not compiled (it's a script file). So @mudasobwa's suggestion is a good one. But if you're not using a mix file then I'd suggest you add one. Commented Feb 28, 2017 at 14:43

2 Answers 2

21

c("user.exs") compiles the file in memory and does not write the bytecode (.beam file) to disk while h/1 currently requires (details below) the beam file to be present on disk to work. You can make c store the generated bytecode in the current directory which will make h/1 work with c("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 relies on Code.get_docs/2 to fetch the documentation which calls :code.get_object_code/1 on the module. :code.get_object_code/1 according to its docs, "Searches the code path for the object code of module Module. Returns {Module, Binary, Filename} if successful, otherwise error."

Sign up to request clarification or add additional context in comments.

Comments

12

The reason is that *.exs files are for scripting and they won't be compiled and *.ex files will be compiled by elixir.

If you have no mix project and the user.ex file only then try elixirc user.ex and after this start iex and type h User.

If you have a mix project then start iex like so from the command line: iex -S mix This will load your project and compiles all *.ex files. Now type h User.

I tried both ways by myself and both work.

See also:

1 Comment

Note that the extension doesn't matter with elixirc. elixirc user.exs also works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.