In the same way that we can get any object (or class) in Ruby to list its methods, is there any function in Elixir to list all functions belonging to a module? Something (a
Each module in Elixir defines an __info__
function you can call to get information about that module.
According the Elixir Docs, 1.6.6 e.g., you can pass it :functions
to get a list of functions that module contains.
Map.__info__(:functions)
[delete: 2, drop: 2, equal?: 2, fetch: 2, fetch!: 2, from_struct: 1, get: 2,
get: 3, has_key?: 2, keys: 1, merge: 2, merge: 3, new: 0, pop: 2, pop: 3,
put: 3, put_new: 3, size: 1, split: 2, take: 2, to_list: 1, update: 4,
update!: 3, values: 1]
I've been using iex(1)> exports TargetModuleName
. It lists all functions and macros belonging to a module. I stumbled onto it trying to figure out how to stop Map.__info__(:functions)
from truncating a long function list.
Since Elixir is also Erlang, there is an Erlang way to do this as well.
Every Elixir and Erlang module has the function module_info
defined at compile
time. There are two arities of this function. For example:
iex(1)> Atom.module_info
[module: Atom,
exports: [__info__: 1, to_string: 1, to_char_list: 1, module_info: 0,
module_info: 1], attributes: [vsn: [66271031909514292894123295368320335064]],
compile: [options: [:debug_info], version: '6.0.1',
time: {2015, 9, 29, 2, 34, 37},
source: '/private/tmp/elixir20150928-10892-fvin6a/elixir-1.1.1/lib/elixir/lib/atom.ex'],
native: false,
md5: <<49, 219, 86, 35, 141, 153, 70, 174, 245, 100, 68, 5, 62, 231, 60, 216>>]
You can specify a specific attribute to return.
iex(2)> Atom.module_info(:exports)
[__info__: 1, to_string: 1, to_char_list: 1, module_info: 0, module_info: 1]
Erlang function verison:
iex(3)> :crypto.module_info(:exports)
[version: 0, stop: 0, supports: 0, info_lib: 0, hash: 2, hash_init: 1,
hash_update: 2, hash_final: 1, hmac: 3, hmac: 4, hmac_init: 2, hmac_update: 2,
hmac_final: 1, hmac_final_n: 2, block_encrypt: 4, block_encrypt: 3,
block_decrypt: 3, next_iv: 2, next_iv: 3, stream_init: 3, stream_init: 2,
stream_encrypt: 2, stream_decrypt: 2, rand_bytes: 1, strong_rand_bytes: 1,
rand_bytes: 3, rand_uniform: 2, rand_seed: 1, mod_pow: 3, verify: 5, sign: 4,
public_encrypt: 4, private_decrypt: 4, private_encrypt: 4, public_decrypt: 4,
exor: 2, generate_key: 2, generate_key: 3, compute_key: 4, md5: 1, md5_init: 0,
md5_update: 2, md5_final: 1, md4: 1, md4_init: 0, md4_update: 2, md4_final: 1,
sha: 1, sha_init: 0, sha_update: 2, ...]
This is what the IEx autocomplete function uses that allows you to expand both Elixir and Erlang functions.