ExUtils.Typespecs (ExUtils v1.0.1)

Copy Markdown View Source

Elixir utility methods for typespecs.

Summary

Types

Supported literals for dynamic typespec generation

Functions

Builds a union type from a list of static values.

Types

literal()

@type literal() :: atom() | integer()

Supported literals for dynamic typespec generation

Functions

from_list(values)

@spec from_list([literal()]) :: Macro.t()

Builds a union type from a list of static values.

This is useful for generating typespecs for enums when the values are already known at compile-time. To use it, you must call this inside a @type definition inside an unquote(...) expression.

Raises an error for invalid literals.

Examples

# Given a list of static values:
@statuses [:open, :closed]
@priorities [1, 2, 3]

# You can use them in a @type definition:
@type status :: unquote(Typespecs.from_list(@statuses))
@type priority :: unquote(Typespecs.from_list(@priorities)) | nil

The above is equivalent to writing these typespecs by hand:

@type status :: :open | :closed
@type priority :: 1 | 2 | 3 | nil