Module

This module provides many functions to deal with modules during compilation time. It allows a developer to dynamically attach documentation, add, delete and register attributes and so forth.

After a module is compiled, using many of the functions in this module will raise errors, since it is out of their purpose to inspect runtime data. Most of the runtime data can be inspected via the __info__(attr) function attached to each compiled module.

Module attributes

Each module can be decorated with one or more attributes. The following ones are currently defined by Elixir:

The following attributes are part of typespecs and are also reserved by Elixir (see Kernel.Typespec for more information about typespecs):

In addition to the built-in attributes outlined above, custom attributes may also be added. A custom attribute is any valid identifier prefixed with an @ and followed by a valid Elixir value:

  defmodule M do
    @custom_attr [some: "stuff"]
  end

For more advanced options available when defining custom attributes, see register_attribute/3.

Runtime information about a module

It is possible to query a module at runtime to find out which functions and macros it defines, extract its docstrings, etc. See __info__/1.

Source

Summary

__info__(kind)

Provides runtime information about functions and macros defined by the module, enables docstring extraction, etc

add_doc(module, line, kind, tuple, signature \\ [], doc)

Attaches documentation to a given function or type. It expects the module the function/type belongs to, the line (a non negative integer), the kind (def or defmacro), a tuple representing the function and its arity, the function signature (the signature should be omitted for types) and the documentation, which should be either a binary or a boolean

concat(list)

Concatenates a list of aliases and returns a new alias

concat(left, right)

Concatenates two aliases and returns a new alias

create(module, quoted, opts \\ [])

Creates a module with the given name and defined by the given quoted expressions. The line where the module is defined and its file can be passed as options

defines?(module, tuple)

Checks if the module defines the given function or macro. Use defines?/3 to assert for a specific type

defines?(module, tuple, kind)

Checks if the module defines a function or macro of the given kind. kind can be any of :def, :defp, :defmacro or :defmacrop

definitions_in(module)

Return all functions defined in module

definitions_in(module, kind)

Returns all functions defined in module, according to its kind

delete_attribute(module, key)

Deletes all attributes that match the given key

eval_quoted(module, quoted, binding \\ [], opts \\ [])

Evaluates the quoted contents in the given module's context

function(mod, fun, arity)

Gets an anonymous function from the given module, function and arity. The module and function are not verified to exist

get_attribute(module, key, warn \\ nil)

Gets the given attribute from a module. If the attribute was marked with accumulate with Module.register_attribute, a list is always returned

make_overridable(module, tuples)

Makes the given functions in module overridable. An overridable function is lazily defined, allowing a developer to customize it. See Kernel.defoverridable for more information and documentation

open?(module)

Check if a module is open, i.e. it is currently being defined and its attributes and functions can be modified

overridable?(module, tuple)

Returns true if tuple in module is marked as overridable

put_attribute(module, key, value)

Puts an Erlang attribute to the given module with the given key and value. The semantics of putting the attribute depends if the attribute was registered or not via register_attribute/2

register_attribute(module, new, opts)

Registers an attribute. By registering an attribute, a developer is able to customize how Elixir will store and accumulate the attribute values

safe_concat(list)

Concatenates a list of aliases and returns a new alias only if the alias was already referenced. If the alias was not referenced yet, fails with ArgumentError. It handles char lists, binaries and atoms

safe_concat(left, right)

Concatenates two aliases and returns a new alias only if the alias was already referenced. If the alias was not referenced yet, fails with ArgumentError. It handles char lists, binaries and atoms

split(module)

Split the given module name into binary parts

Functions

__info__(kind)

Provides runtime information about functions and macros defined by the module, enables docstring extraction, etc.

Each module gets an __info__/1 function when it's compiled. The function takes one of the following atoms:

  • :functions - keyword list of public functions along with their arities

  • :macros - keyword list of public macros along with their arities

  • :docs - list of all docstrings attached to functions and macros using the @doc attribute

  • :moduledoc - tuple { <line>, <doc> } where line is the line on which module definition starts and doc is the string attached to the module using the @moduledoc attribute

  • :module - module name (Module == Module.__info__(:module))

In addition to the above, you may also pass to __info__/1 any atom supported by Erlang's module_info function which also gets defined for each compiled module. See http://erlang.org/doc/reference_manual/modules.html#id69430 for more information.

Source
add_doc(module, line, kind, tuple, signature \\ [], doc)

Attaches documentation to a given function or type. It expects the module the function/type belongs to, the line (a non negative integer), the kind (def or defmacro), a tuple representing the function and its arity, the function signature (the signature should be omitted for types) and the documentation, which should be either a binary or a boolean.

Examples

defmodule MyModule do
  Module.add_doc(__MODULE__, __ENV__.line + 1, :def, { :version, 0 }, [], "Manually added docs")
  def version, do: 1
end
Source
concat(list)

Specs:

  • concat([binary | atom]) :: atom

Concatenates a list of aliases and returns a new alias.

Examples

iex> Module.concat([Foo, Bar])
Foo.Bar
iex> Module.concat([Foo, "Bar"])
Foo.Bar
Source
concat(left, right)

Specs:

  • concat(binary | atom, binary | atom) :: atom

Concatenates two aliases and returns a new alias.

Examples

iex> Module.concat(Foo, Bar)
Foo.Bar
iex> Module.concat(Foo, "Bar")
Foo.Bar
Source
create(module, quoted, opts \\ [])

Creates a module with the given name and defined by the given quoted expressions. The line where the module is defined and its file can be passed as options.

Examples

contents =
  quote do
    def world, do: true
  end

Module.create(Hello, contents, __ENV__.location)

Hello.world #=> true

Differences from defmodule

Module.create works similarly to defmodule and return the same results. While one could also use defmodule to define modules dynamically, this function is preferred when the module body is given by a quoted expression.

Another important distinction is that Module.create allows you to control the environment variables used when defining the module, while defmodule automatically shares the same environment.

Source
defines?(module, tuple)

Checks if the module defines the given function or macro. Use defines?/3 to assert for a specific type.

Examples

defmodule Example do
  Module.defines? __MODULE__, { :version, 0 } #=> false
  def version, do: 1
  Module.defines? __MODULE__, { :version, 0 } #=> true
end
Source
defines?(module, tuple, kind)

Checks if the module defines a function or macro of the given kind. kind can be any of :def, :defp, :defmacro or :defmacrop.

Examples

defmodule Example do
  Module.defines? __MODULE__, { :version, 0 }, :defp #=> false
  def version, do: 1
  Module.defines? __MODULE__, { :version, 0 }, :defp #=> false
end
Source
definitions_in(module)

Return all functions defined in module.

Examples

defmodule Example do
  def version, do: 1
  Module.definitions_in __MODULE__ #=> [{:version,0}]
end
Source
definitions_in(module, kind)

Returns all functions defined in module, according to its kind.

Examples

defmodule Example do
  def version, do: 1
  Module.definitions_in __MODULE__, :def  #=> [{:version,0}]
  Module.definitions_in __MODULE__, :defp #=> []
end
Source
delete_attribute(module, key)

Deletes all attributes that match the given key.

Examples

defmodule MyModule do
  Module.put_attribute __MODULE__, :custom_threshold_for_lib, 10
  Module.delete_attribute __MODULE__, :custom_threshold_for_lib
end
Source
eval_quoted(module, quoted, binding \\ [], opts \\ [])

Evaluates the quoted contents in the given module's context.

A list of environment options can also be given as argument. See Code.eval_string for more information.

Raises an error if the module was already compiled.

Examples

defmodule Foo do
  contents = quote do: (def sum(a, b), do: a + b)
  Module.eval_quoted __MODULE__, contents
end

Foo.sum(1, 2) #=> 3

For convenience, you can my pass __ENV__ as argument and all options will be automatically extracted from the environment:

defmodule Foo do
  contents = quote do: (def sum(a, b), do: a + b)
  Module.eval_quoted __MODULE__, contents, [], __ENV__
end

Foo.sum(1, 2) #=> 3
Source
function(mod, fun, arity)

Gets an anonymous function from the given module, function and arity. The module and function are not verified to exist.

iex> fun = Module.function(Kernel, :is_atom, 1)
iex> fun.(:hello)
true
Source
get_attribute(module, key, warn \\ nil)

Specs:

  • get_attribute(module, atom, warn :: nil | [tuple]) :: term

Gets the given attribute from a module. If the attribute was marked with accumulate with Module.register_attribute, a list is always returned.

The @ macro compiles to a call to this function. For example, the following code:

@foo

Expands to:

Module.get_attribute(__MODULE__, :foo, true)

Notice the third argument may be given to indicate a stacktrace to be emitted when the attribute was not previously defined. The default value for warn is nil for direct calls but the @foo macro sets it to the proper stacktrace automatically, warning every time @foo is used but not set previously.

Examples

defmodule Foo do
  Module.put_attribute __MODULE__, :value, 1
  Module.get_attribute __MODULE__, :value #=> 1

  Module.register_attribute __MODULE__, :value, accumulate: true
  Module.put_attribute __MODULE__, :value, 1
  Module.get_attribute __MODULE__, :value #=> [1]
end
Source
make_overridable(module, tuples)

Makes the given functions in module overridable. An overridable function is lazily defined, allowing a developer to customize it. See Kernel.defoverridable for more information and documentation.

Source
open?(module)

Check if a module is open, i.e. it is currently being defined and its attributes and functions can be modified.

Source
overridable?(module, tuple)

Returns true if tuple in module is marked as overridable.

Source
put_attribute(module, key, value)

Puts an Erlang attribute to the given module with the given key and value. The semantics of putting the attribute depends if the attribute was registered or not via register_attribute/2.

Examples

defmodule MyModule do
  Module.put_attribute __MODULE__, :custom_threshold_for_lib, 10
end
Source
register_attribute(module, new, opts)

Registers an attribute. By registering an attribute, a developer is able to customize how Elixir will store and accumulate the attribute values.

Options

When registering an attribute, two options can be given:

  • :accumulate - Several calls to the same attribute will accumulate instead of override the previous one. New attributes are always added to the top of the accumulated list.

  • :persist - The attribute will be persisted in the Erlang Abstract Format. Useful when interfacing with Erlang libraries.

By default, both options are false.

Examples

defmodule MyModule do
  Module.register_attribute __MODULE__,
    :custom_threshold_for_lib,
    accumulate: true, persist: false

  @custom_threshold_for_lib 10
  @custom_threshold_for_lib 20
  @custom_threshold_for_lib #=> [20, 10]
end
Source
safe_concat(list)

Specs:

  • safe_concat([binary | atom]) :: atom | no_return

Concatenates a list of aliases and returns a new alias only if the alias was already referenced. If the alias was not referenced yet, fails with ArgumentError. It handles char lists, binaries and atoms.

Examples

iex> Module.safe_concat([Unknown, Module])
** (ArgumentError) argument error

iex> Module.safe_concat([List, Chars])
List.Chars
Source
safe_concat(left, right)

Specs:

  • safe_concat(binary | atom, binary | atom) :: atom | no_return

Concatenates two aliases and returns a new alias only if the alias was already referenced. If the alias was not referenced yet, fails with ArgumentError. It handles char lists, binaries and atoms.

Examples

iex> Module.safe_concat(Unknown, Module)
** (ArgumentError) argument error

iex> Module.safe_concat(List, Chars)
List.Chars
Source
split(module)

Split the given module name into binary parts.

Examples

Module.split Very.Long.Module.Name.And.Even.Longer
#=> ["Very", "Long", "Module", "Name", "And", "Even", "Longer"]
Source