How to display typespecs from a named function through iex

781 views Asked by At

You can display typespecs from a module thorugh t command on iex, i.e:

iex(1)> t Enum
@type t() :: Enumerable.t()
@type acc() :: any()
@type element() :: any()
@type index() :: integer()
@type default() :: any()

But how can I see the typespecs from for example Enum.reverse?

If I go to the source code then I see

  @spec reverse(t) :: list
  def reverse(enumerable)

Where I suppose t stands for Enum itself @type t() :: Enumerable.t() and expect to return a list

In python for example you can get a method doc through the shell with ?? (python doesnt implement typespecs but you get the idea)

In [1]: from urllib2 import urlparse
In [2]: urlparse??
def urlparse(url, scheme='', allow_fragments=True):
    """Parse a URL into 6 components:
    <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
    Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
    Note that we don't break the components up in smaller bits
    (e.g. netloc is a single string) and we don't expand % escapes."""
1

There are 1 answers

6
Dogbert On BEST ANSWER

IEx.Helpers.h/1 prints the typespec along with the documentation of the function:

iex(1)> h Enum.reverse/1

                            def reverse(enumerable)

    @spec reverse(t()) :: list()

Returns a list of elements in enumerable in reverse order.

## Examples

    iex> Enum.reverse([1, 2, 3])
    [3, 2, 1]

After a quick read of the IEx.Helpers module, I don't think there's a builtin helper function that only prints the typespec of a function.

Edit: @spec is not printed in the current stable release of Elixir (1.5.3). This feature will be present in Elixir 1.6.