jonar...@gmail.com
unread,Apr 13, 2021, 11:02:59 AM4/13/21Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to elixir-lang-core
While working with the v1.12.0 rc0 release, I wrote the following typespec:
@spec my_fun(1..10//2) :: boolean()
which resulted in the following error:
== Compilation error in file lib/foobar.ex ==
** (CompileError) lib/foobar.ex:6: type ..///3 undefined (no such type in Foobar)
My confusion stemmed from my belief that ranges in typespecs were interpreted by Elixir and expanded at compile time (e.g. 1..10 to 1|2|3|4|5|...) since the syntax (first..last) matches the Elixir range syntax and Erlang has no native ranges. Of course, this was wrong because while Erlang doesn't have ranges, Dialyzer does.
While I can't think of the last time I wanted the contract of my function to be "only accepts odd integers between 1 and 100", it also seems like a curious omission. From the perspective of someone with good knowledge of Elixir but limited knowledge of Erlang and Dialyzer, I had originally believed that this was some kind of mistake or oversight.
There also might be a use-case in providing additional information to Dialyzer, such as:
@spec odd?(1..10//2) :: true
@spec odd?(2..10//2) :: false
Would it be worth taking a type such as 1..10//2 and compiling it to 1|3|5|7|9 for Dialyzer?