Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Elixir, is there any way to get a module to list its functions?

Tags:

elixir

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 (at least remotely) like String.functions (where String could be replaced by any other module name)?

like image 916
iconoclast Avatar asked Feb 22 '15 22:02

iconoclast


People also ask

What is __ module __ In Elixir?

__MODULE__ is a compilation environment macros which is the current module name as an atom. Now you know alias __MODULE__ just defines an alias for our Elixir module. This is very useful when used with defstruct which we will talk about next. In the following example, we pass API.

What does |> mean in Elixir?

This is the pipe operator. From the linked docs: This operator introduces the expression on the left-hand side as the first argument to the function call on the right-hand side. Examples.


1 Answers

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] 
like image 73
Steven Schobert Avatar answered Sep 20 '22 23:09

Steven Schobert