<div dir="ltr"><div><div><div>I have done some more checking and the problem still remains. These following type specifications are all legal in the sense that compiler does not complain about them or it gives a strange error:<br><br>-spec f5(_) -> integer() when atom(integer()).<br>-spec f6(Y) -> integer() when atom(Y :: integer()).<br><br></div>The first is passed by the compiler without any warnings or errors while the second you get the error "type variable 'Y' is only used once (is unbound)". So if the first is legal then what does it mean? And why is the second illegal because Y occurs more than once?<br><br></div>Does this mean I should strictly only accept exactly what is in the manual and not what lint accepts?<br><br></div>Robert<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 3 October 2016 at 19:16, Fred Hebert <span dir="ltr"><<a href="mailto:mononcqc@ferd.ca" target="_blank">mononcqc@ferd.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 10/03, Robert Virding wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
And it if can be used in a<br>
meaningful way why isn't it documented? I think that having syntax which<br>
can never legal is a great way to complicate things. Which we don't need.<br>
</blockquote>
<br></span>
The three samples for syntax were:<br>
<br>
-type atom(X) :: list(X).<span class=""><br>
-spec foo(Y) -> integer() when atom(Y).<br>
-spec foo(Y) -> integer() when atom(Y :: integer()).<br>
<br></span>
So here's a valid way to use them that is useful, at the very least in some cases:<br>
<br>
   -module(mod).<br>
   -export([main/0]).<br>
      -type collection(K, V) :: #{K => V}<br>
                           | dict:dict(K, V)<br>
                           | gb_trees:tree(K, V).<br>
      -spec lookup(K,F,C) -> V when<br>
                 C :: collection(K, V),<br>
                 F :: fun((K, C) -> V).<br>
   lookup(K, F, C) -> F(K, C).<br>
      main() -><br>
       C = maps:from_list([{a,1},{b,2},{c<wbr>,3}]),<br>
       lookup(b, fun maps:get/2, C),<br>
       lookup("bad ignored type", fun maps:get/2, C),<br>
       lookup(b, C, fun maps:get/2).<br>
<br>
This module defines an accessor function using the parametrized types and 'when' parts of typespecs syntax. Sadly the analysis is currently not good enough to figure out that "bad ignored type" is not an acceptable value of `K' for the lookup (it appears dialyzer does not do the parametrized inference this deep), but in the third call, it can definitely infer that I swapped the collection and the accessor function:<br>
<br>
   mod.erl:13: Function main/0 has no local return<br>
   mod.erl:17: The call mod:lookup('b',C::#{'a'=>1, 'b'=>2,    'c'=>3},fun((_,_) -> any())) does not have a term of type    dict:dict(_,_) | gb_trees:tree(_,_) | map() (with opaque subterms)    as 3rd argument<br>
<br>
Had I otherwise defined my spec as:<br>
<br>
   -spec lookup(K,F,C) -> V when<br>
                 K :: atom(),<br>
                 C :: collection(K, V),<br>
                 F :: fun((K, C) -> V).<br>
<br>
Which adds a constraint that keys must be atoms, Then dialyzer would have caught my error:<br>
<br>
   mod.erl:17: The call mod:lookup("bad ignored type",fun((_,_) ->    any()),C::#{'a'=>1, 'b'=>2, 'c'=>3}) breaks the contract (K,F,C) ->    V when K :: atom(), C :: collection(K,V), F ::    fun((K,collection(K,V)) -> V)<br>
<br>
If you're using rebar3, you should also be getting colored rebar3 output, which does make the error a lot easier to see by putting the bad value in red and the piece of contract it breaks in green*: <a href="http://i.imgur.com/AjNgVCB.png" rel="noreferrer" target="_blank">http://i.imgur.com/AjNgVCB.png</a><br>
<br>
Regards,<br>
Fred.<br>
<br>
* we should find a way to somehow parametrize the colors to help * colorblind users I guess.<br>
</blockquote></div><br></div>