Last active
June 29, 2023 04:17
-
-
Save woylie/d1fcb95fd472a97fd8e0ef6f80dda8ec to your computer and use it in GitHub Desktop.
Convert list of values to type specification in Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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