1

I wanted to know if there was a standard NotImplementedError I could pass to Kernel.raise/1. I was curious whether I could figure out (beyond just trying it) whether that module existed.

More generally, it seems useful to be able to search or even just list all of the 'available' modules for an iex session.

This question is similar to this other question:

But that question is asking about "the [Elixir] standard lib modules". I want to retrieve or generate an (Elixir) list (i.e. an Elixir value I can manipulate further) of all modules loaded-in or otherwise 'available' in a specific iex session.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
  • 1
    Does this answer your question? [Get a list of all elixir modules in IEx](https://stackoverflow.com/questions/58461572/get-a-list-of-all-elixir-modules-in-iex) – Adam Millerchip Nov 19 '19 at 01:43
  • @AdamMillerchip Yes and no. Your answer does result in the modules being *printed* in `iex`. Because I wasn't sure of the 'full' name of the module I was looking for, or even whether it existed [it didn't], I really wanted to *search* the list of modules, not just print them. And that question seems to be about listing "the standard lib modules" – this question is about listing all modules loaded-in or otherwise available from a specific `iex` session. – Kenny Evitt Nov 19 '19 at 16:22

2 Answers2

4

Answering the question stated

I was curious whether I could figure out (beyond just trying it) whether that module existed.

there is Code.ensure_loaded?/1 which does exatly this.

Code.ensure_loaded?(Atom)
#⇒ true
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • I don't think this would have quite helped as I wasn't sure of the *full* name of the module, i.e. whether it was `Elixir.NotImplementedError` or something else. – Kenny Evitt Nov 19 '19 at 15:54
  • And yes, I know there's no 'full' or 'short' names – the name *is* the full name in Elixir. But that's why this answer doesn't help me. – Kenny Evitt Nov 19 '19 at 16:26
0

I found this post on Elixir Forum:

A comment pointed to a function in the IEx.Autocomplete module in the Elixir source:

Here are the functions that function uses, all just below it in the same file:

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() seems like what I want:

Returns a list of tuples {Module, Loaded} for all loaded modules. Loaded is normally the absolute filename, as described for is_loaded/1.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
  • 1
    Beware that `:ac_tab` is implementation internals and nobody promised it’d be compatible with future versions / releases. – Aleksei Matiushkin Nov 19 '19 at 04:43
  • @AlekseiMatiushkin Good point! I'd imagine just using `:code.all_loaded()` itself would usually be sufficient. Otherwise, I'd imagine tracking the `iex` code in the Elixir source would point anyone that needs to examine the modules in loaded applications in the right direction. – Kenny Evitt Nov 19 '19 at 15:51
  • 1
    If you are after looking up the existing module from core, got to `https://hexdocs.pm/elixir` and start typing in the search field. It has smart autocomplete. Whatever is not documented is considered the implementation internals _and_ might have changed without a notice in the future releases. – Aleksei Matiushkin Nov 19 '19 at 15:56