[erlang-questions] dialyzer -Wunderspecs and the binary() type

James Aimonetti james@REDACTED
Sat Oct 1 19:00:43 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks for the clarification Kostis. Always nice to deepen my
understanding of how to use Dialyzer more effectively.

James

On 09/29/2011 11:41 PM, Kostis Sagonas wrote:
> On 09/29/11 21:27, James Aimonetti wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> We use -Wunderspecs a lot in our Dialyzing and the only minor complaint
>> I have is when I get warnings that the success typing is more specfic
>> with the binary() type.
>>
>> We see a lot of:
>>
>> foo.erl:123: Type specfication foo:a_fun(BinVal) ->  'ok' when
>> is_subtype(BinVal, binary()) is a supertype of the success typing:
>> foo:a_fun(<<_:8,_:_*8>>) ->  'ok'.
>>
>> In foo.erl, a_fun/1 is defined as:
>>
>> - -spec a_fun(BinVal) ->  'ok' when
>>    BinVal :: binary().
>> a_fun(BinVal) ->
>>    %% do something with BinVal
>>    ok.
>>
>> This is obviously a simplified, trivial example.
> 
> It's not just a simplified example.  It's an example that is incomplete
> because it does not show the problem.  Here is one that does:
> 
>   -module(foo).
>   -export([a_fun/1]).
> 
>   -spec a_fun(binary()) -> 'ok'.
>   a_fun(B) ->
>     do(B), ok.
> 
>   do(<<_>>) -> ok;
>   do(<<_,R/binary>>) -> do(R).
> 
> Note that the first clause of do/1 has a non-empty binary there (there
> is an underscore). From this clause dialyzer will infer that the do/1
> function only returns if its argument is a non-empty binary of at least
> one byte.
> 
>> Just curious if its
>> possible to have Dialyzer *not* output the -Wunderspecs warnings for
>> binaries?
> 
> Why on earth would we want to special case this?

> 
>> Or if there's a better way to specify binary inputs/outputs
>> (since not all of our functions using binary() in the spec have this
>> issue).
> 
> As I explained, the warning is related to the fact that these functions
> only accept non-empty binaries. (Whether this is intentional or not in
> your code I will let you decide.)  To suppress these warnings I
> recommend the following: define a non-empty binary type
> 
>   -type ne_binary() :: <<_:8,_:_*8>>.
> 
> and change the spec of your a_fun above to:
> 
>   -spec a_fun(ne_binary()) -> 'ok'.
> 
> (Aside: I do not see the need to use 'when' for type variables that only
> appear once in a spec. The non when form is shorter and nicer.)
> 
> Kostis
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 


- -- 
James Aimonetti
Distributed Systems Engineer / DJ MC_

2600hz | http://2600hz.com
sip:james@REDACTED
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOh0c7AAoJENc77s1OYoGgrUcH/26BfA8xBLQ95H4541KazZ7M
i5mHR4qFItD0+L1P6x/SfoOr0KFkAhoggyawUmh6pHki7l8Jq1Y5VvRDSOrJR2uU
4Ob18pENuQonvgwv7jxZLKANl67Q58oglAfJVh6FGq+4C5ksIXsqKtNl5PGOSyCE
E1AAGkMzmsJqGEBSBiujPM0IZF/9JCSfLI6FRu/LP2gAY8pijJRaJLYX0Ubv3sbU
0cRB+UIR4pteWiytAdsGoSlaOpmm4yrBNfIyBFMqaR7B+ki3fv+VP5RmiFcqb16q
ujVWz0xID/iU92nZm3nT+DGmPzUkTiNaZ11N6p76yzLc9mtka4JNfunfIicw/Js=
=jXxw
-----END PGP SIGNATURE-----



More information about the erlang-questions mailing list