[Proposal] Allow multiple clauses for a type

14 views
Skip to first unread message

Rafael Willians Moraes

unread,
Mar 30, 2023, 12:02:59 AM3/30/23
to elixir-lang-core
Right now it's possible to declare multiple clauses for `@spec` as we can see in Typespecs documentation, but we don't have the same feature for `@type` and `@typep`.

Since `@spec` allows for it, one -- me, actually -- would think the same could be done to `@type(p)`. Here's a silly example for when this would be helpful:

```
@type error_details(t) :: {:invalid, :invalid_atom, term} when t: atom
@type error_details(t) :: {:invalid, :invalid_boolean, term} when t: binary
@type error_details(t) :: {:invalid, :invalid_whatever, term} when t: user_custom_type

@spec foo(atom) :: {:ok, binary} | {:error, error(atom)
@spec foo(binary) :: {:ok, binary} | {:error, error(binary)
@spec foo(user_custom_type) :: {:ok, binary} | {:error, error(user_custom_type)
def foo(value)
```

IMO, having this feature for `@type` (prob for `@typep` as well, just for consistency) is complementary to multiple `@spec` clauses, allowing for reuse of typespecs.

-


José Valim

unread,
Mar 30, 2023, 1:26:03 AM3/30/23
to elixir-l...@googlegroups.com
Is this feature supported in Erlang? I am afraid it is not, which means we can’t support it.

--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/24393ca9-b21b-4ef6-a0cc-c4d333be0d98n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages