Skip to content

Instantly share code, notes, and snippets.

@woylie
Last active June 29, 2023 04:17
Show Gist options
  • Select an option

  • Save woylie/d1fcb95fd472a97fd8e0ef6f80dda8ec to your computer and use it in GitHub Desktop.

Select an option

Save woylie/d1fcb95fd472a97fd8e0ef6f80dda8ec to your computer and use it in GitHub Desktop.
Convert list of values to type specification in Elixir
@doc """
Converts a list of values into an AST for defining a type.
Source: https://elixirforum.com/t/dynamically-generate-typespecs-from-module-attribute-list/7078/16
## Usage
@categories [:one, :two, :three]
@type category :: unquote(list_to_typespec(@categories))
"""
def list_to_typespec(list) when is_list(list) do
Enum.reduce(list, &{:|, [], [&1, &2]})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment