你可以在 iex 之外使用自省功能吗?

use*_*324 2 bash elixir

我可以在 iex 之外使用 i() 吗?还有其他只能在 iex 中访问的功能吗?

iex> i(3)
Term
  3
Data type
  Integer
Reference modules
  Integer
Implemented protocols
  IEx.Info, Inspect, List.Chars, String.Chars

bash> elixir -e "i(3)"
** (CompileError) nofile:1: undefined function i/1
(elixir 1.11.1) lib/code.ex:341: Code.eval_string_with_error_handling/3
Run Code Online (Sandbox Code Playgroud)

Bre*_*tty 5

i/1IEx.Helpers.i/1的导入,因此当它可用时,您可以使用完全限定名称:

$ elixir -e 'IEx.Helpers.i(3)'
Term
  3
Data type
  Integer
Reference modules
  Integer
Implemented protocols
  IEx.Info, Inspect, List.Chars, String.Chars
Run Code Online (Sandbox Code Playgroud)

如果你想在自己的应用程序中使用它的一些原因,一定要添加:iex:extra_applications您的mix.exs。否则,您将收到有关您的应用程序不直接依赖于:iex应用程序的警告(可能只是新版本的 Elixir)。

为了回答您的第二个问题,我想IEx.Helpers中的所有助手都旨在仅在 IEx 中使用。您可以通过调用在 IEx 中看到这些h(),它是从IEx.Helpers.h/0导入的,它打印IEx.Helpers的文档。