Today I was reading through http://learnyousomeerlang.com/dialyzer and 
stumbled across this:

-spec convert(tuple()) -> list();
  (list()) -> tuple().convert(Tup) when is_tuple(Tup) -> 
tuple_to_list(Tup);convert(L = [_|_]) -> list_to_tuple(L).

Rather than putting tuple() and list() types together into a single union, 
this syntax allows you to declare type signatures with alternative clauses. 
If you call convert/1 with a tuple, we expect a list, and the opposite in 
the other case.

I’ve never seen this in Elixir typespecs before and the docs 
<http://elixir-lang.org/docs/stable/elixir/typespecs> make no mention of 
Elixir supporting multiple clauses in a type spec. Is this something Elixir 
supports (or could it in the future)? I can imagine a syntax like:

@spec convert(tuple) :: list@spec convert(list) :: tupledef convert(tup) when 
is_tuple(tup), do: Tuple.to_list(tup)def convert([_ | _] = list), do: 
List.to_tuple(list)

Basically, just allowing us to list multiple type spec clauses sequentially 
like we do with function clauses, with line breaks in between.

If this is already supported, I can work on updating the type spec docs to 
mention it.

Thanks,
Myron
​

-- 
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/6bf79af0-6f26-42f7-83e6-35c7f0b7c37f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to