Today you can do it like this:

var = Enum.reduce(@list, &{:|, _. [&1, &2]}
@type foo :: unquote(var)

But it may be worth introducing something like you describe but it probably
makes sense to do it via a construct, such as @type foo :: one_of(@var)
that does the conversion for you. I think automatically converting the list
can be confusing, because people may think that [:foo, :bar] implies a
specific ordering, for example.

I am just not sure of a good name. one_of may conflict with existing code.

On Sat, May 16, 2020 at 6:25 AM Kip <[email protected]> wrote:

> It's not uncommon to have domain overlap between lists of valid tokens
> (used for validations) and type specs. For example:
>
>   @standard_formats [
>     :standard,
>     :accounting,
>     :currency,
>     :percent
>   ]
>
>   @currency_formats [
>     :currency,
>     :accounting,
>     :currency_long,
>     :currency_short
>   ]
>
>   @currency_symbol [
>     :standard,
>     :iso
>   ]
>
>   @type standard_formats :: :standard | :currency | :accounting | :short |
> :long
>   @type currency_formats ::  :currency_short | :currency_long |
> :decimal_short | :decimal_long
>   @type currency_symbol :: :standard | :iso
>
> It would go good to remove one source of error by being able to allow
> compile time use of a list as the subject of  @typespec. For example:
>
> @type currency_symbol :: @currency_symbol # Any compile-time resolvable
> list
>
> Of course a macro can be introduced to do this but its quite difficult to
> achieve since it requires manual manipulation of AST.
>
> Proposal worth considering? Or consign to the history of my
> less-than-helpful ideas :-)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/36f821aa-d718-48aa-a9e8-2f6d5e440632%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/36f821aa-d718-48aa-a9e8-2f6d5e440632%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BRfGarTtrtQ8aeYkLPxWNA_h%3DuggUn%3DWYRiTO7Evr6dg%40mail.gmail.com.

Reply via email to