In Elixir, is there a way to determine if a module exists?

Usually, we just check to ensure a given function in the module is compiled.

iex(9)> Code.ensure_compiled?(Enum)
true
iex(10)>

You can also check to see if a specific function is definined

ex(10)> function_exported? Enum, :count, 1
true
iex(11)>

EDIT

@Russ Matney as a good point about Code.ensure_compiled?/1 loading the module.

Here is an approach that should work without any side effects:

defmodule Utils do
  def module_compiled?(module) do
    function_exported?(module, :__info__, 1)
  end
end

iex> Utils.module_compiled?(String)
true
iex> Utils.module_compiled?(NoModule)
false

Elixir modules export :__info__/1 so testing for it provides a generic solution.

Tags:

Elixir