如何在“ iex”中检索所有已加载的模块?

时间:2019-11-18 21:48:31

标签: elixir iex

我想知道是否可以将标准NotImplementedError传递给Kernel.raise/1。我很好奇我是否可以找出(除了尝试之外)该模块是否存在。

更一般地说,能够搜索甚至列出iex会话的所有“可用”模块似乎很有用。

这个问题与另一个问题类似:

但是这个问题是关于“ [Elixir]标准lib模块”的。我想检索或生成在特定iex会话中已加载或“可用”的所有模块的(Elixir)列表(即,我可以进一步操作的Elixir值)。 / p>

3 个答案:

答案 0 :(得分:2)

回答提出的问题

  

我很好奇是否可以(除了尝试之外)弄清楚该模块是否存在。

Code.ensure_loaded?/1确实做到了。

Code.ensure_loaded?(Atom)
#⇒ true

答案 1 :(得分:0)

我在Elixir论坛上找到了这篇文章:

注释指向Elixir源中IEx.Autocomplete模块中的功能:

以下是该函数使用的功能,所有功能均位于同一文件的下方:

defp get_modules(true) do
    ["Elixir.Elixir"] ++ get_modules(false)
  end

  defp get_modules(false) do
    modules = Enum.map(:code.all_loaded(), &Atom.to_string(elem(&1, 0)))

    case :code.get_mode() do
      :interactive -> modules ++ get_modules_from_applications()
      _otherwise -> modules
    end
  end

  defp get_modules_from_applications do
    for [app] <- loaded_applications(),
        {:ok, modules} = :application.get_key(app, :modules),
        module <- modules do
      Atom.to_string(module)
    end
  end

  defp loaded_applications do
    # If we invoke :application.loaded_applications/0,
    # it can error if we don't call safe_fixtable before.
    # Since in both cases we are reaching over the
    # application controller internals, we choose to match
    # for performance.
    :ets.match(:ac_tab, {{:loaded, :"$1"}, :_})
  end

:code.all_loaded()似乎是我想要的:

  

返回所有已加载模块的元组{Module, Loaded}的列表。 Loaded通常是绝对文件名,如is_loaded/1所述。

答案 2 :(得分:0)

在IEx中,您可以输入const RecursiveWrapper = props => { const wrappedChildren = React.Children.map( props.children, child => { const type = child.type === 'img' ? 'ReactIntense' : child.type if (child.props && child.props.children) { return React.cloneElement( { ...child, type // substitute original type }, { ...child.props, // Wrap grandchildren too children: ( <RecursiveWrapper> {child.props.children} </RecursiveWrapper> ) } ) } return child } ); return ( <> {wrappedChildren} </> ) } (...) <RecursiveWrapper> <Gallery photos={imageDicts} direction="column" columns={columns} /> </RecursiveWrapper> + :以获得所有可用模块的列表。