[erlang-questions] Dialyzer and binary() supertypes

33 views
Skip to first unread message

Olav Frengstad

unread,
Aug 27, 2012, 5:06:54 PM8/27/12
to erlang-q...@erlang.org
Hey,

I have the following snippet of code which I'm trying to get Dialyzer to pass:

-module(test).

-compile([export_all]).

-spec a({binary(), binary()} | binary()) -> atom().
a(<<"abc/def">>) -> ok1.
a(<<"hijk/lmno">>) -> ok2.

When running the above I get an error regarding the type specification:
[/tmp] erlc +debug_info test.erl && dialyzer test.beam -Wunderspecs
....
test.erl:7: Invalid type specification for function test:a/1. The
success typing is (<<_:56,_:_*16>>) -> 'ok1' | 'ok2'

Am I expected to create a custom type for to match static binary
patterns, or is there some other type I can use for binaries with
length() > 0?

Regards,
Olav
--
Med Vennlig Hilsen
Olav Frengstad

Systemutvikler // FWT
+47 920 42 090
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

James Aimonetti

unread,
Aug 27, 2012, 5:10:42 PM8/27/12
to erlang-q...@erlang.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

My assumption is that Dialyzer is letting you know that {binary(),
binary()} is superfluous and can be removed (at least in the example
you provided), and that you can be even more specific with your
binary() declaration.

- -type ne_binary() :: <<_:8,_:_*8>>.

We use the above type to indicate the function does not accept empty
binaries. See if that helps?

On 08/27/2012 02:06 PM, Olav Frengstad wrote:
> Hey,
>
> I have the following snippet of code which I'm trying to get
> Dialyzer to pass:
>
> -module(test).
>
> -compile([export_all]).
>
> -spec a({binary(), binary()} | binary()) -> atom().
> a(<<"abc/def">>) -> ok1. a(<<"hijk/lmno">>) -> ok2.
>
> When running the above I get an error regarding the type
> specification: [/tmp] erlc +debug_info test.erl && dialyzer
> test.beam -Wunderspecs .... test.erl:7: Invalid type specification
> for function test:a/1. The success typing is (<<_:56,_:_*16>>) ->
> 'ok1' | 'ok2'
>
> Am I expected to create a custom type for to match static binary
> patterns, or is there some other type I can use for binaries with
> length() > 0?
>
> Regards, Olav
>


- --
James Aimonetti
Distributed Systems Engineer / DJ MC_

2600hz | http://2600hz.com
sip:ja...@2600hz.com
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQEcBAEBAgAGBQJQO+JSAAoJENc77s1OYoGg05UIAKs6YrgXUuiL02t1T5Ihj1yo
muu9q69DJsO/0kH+Xi7vcUrSpudeApay6OxCl1RrNR2nsuQA4xtJBZtaEya52wi5
U+gZ/bIptSkG6T3UsNzxlWrNoL21+OGU42KK89R+d/DXOQYJjFC0izZGz1qqIqLs
Mdz6qfTcuPPPMnpUYvxCpwmUEAZe5DyQNvKttQXOQBDx6tH6QNiLE2O3NZ5W+3iU
KIke56oFppGGLsix5Rh8/QZdFEOPxeg5xOMsYujToWINYVLLrnNPHOoJ3joa6KGe
ocoQZJuihY2gCDysA56fXC1yh1ElN5FO8CAB9EFHljAFdiVmSOD+1fv8GVHTO3Q=
=d3gL
-----END PGP SIGNATURE-----

James Aimonetti

unread,
Aug 27, 2012, 5:32:17 PM8/27/12
to Olav Frengstad, Erlang Questions
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Right, but you have -Wunderspecs on, which tells Dialyzer to "Warn
about underspecified functions (the -spec is strictly more allowing
than the success typing)". binary() and ne_binary() allow a wider
range of binary constructions than the detected static binaries.
Remove -Wunderspecs and you won't get this warning (or have a
catch-all clause).

On 08/27/2012 02:25 PM, Olav Frengstad wrote:
> Hey,
>
> Sorry the spec in the first post should be: -spec a(binary()) ->
> ok.
>
> Regarding the ne_binary() type, this does not work as expected
> unless you have something like a(<<_>>) -> ok. I guess dialyzer
> infers the length of all the possible static matches and expects a
> equal length type.
>
> This is not an issue when binding a variable: a(<<X/binary>>) ->
> ok.
>
> Cheers, Olav
>
> 2012/8/27 James Aimonetti <ja...@2600hz.com>: My assumption is that
> Dialyzer is letting you know that {binary(), binary()} is
> superfluous and can be removed (at least in the example you
> provided), and that you can be even more specific with your
> binary() declaration.
>
> -type ne_binary() :: <<_:8,_:_*8>>.
>
> We use the above type to indicate the function does not accept
> empty binaries. See if that helps?
>
> On 08/27/2012 02:06 PM, Olav Frengstad wrote:
>>>> Hey,
>>>>
>>>> I have the following snippet of code which I'm trying to get
>>>> Dialyzer to pass:
>>>>
>>>> -module(test).
>>>>
>>>> -compile([export_all]).
>>>>
>>>> -spec a({binary(), binary()} | binary()) -> atom().
>>>> a(<<"abc/def">>) -> ok1. a(<<"hijk/lmno">>) -> ok2.
>>>>
>>>> When running the above I get an error regarding the type
>>>> specification: [/tmp] erlc +debug_info test.erl && dialyzer
>>>> test.beam -Wunderspecs .... test.erl:7: Invalid type
>>>> specification for function test:a/1. The success typing is
>>>> (<<_:56,_:_*16>>) -> 'ok1' | 'ok2'
>>>>
>>>> Am I expected to create a custom type for to match static
>>>> binary patterns, or is there some other type I can use for
>>>> binaries with length() > 0?
>>>>
>>>> Regards, Olav
>>>>
>
>
>> _______________________________________________ erlang-questions
>> mailing list erlang-q...@erlang.org
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>

- --
James Aimonetti
Distributed Systems Engineer / DJ MC_

2600hz | http://2600hz.com
sip:ja...@2600hz.com
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQEcBAEBAgAGBQJQO+dhAAoJENc77s1OYoGgbxQH/jqTNa+niZ7MOTaaUXs95Zs0
/lKwXT5RQlK/kkvK7gzkloGkBQ0b8s35ESfP6iORjbyMqw56h5dXyS9+x8Ssv2w9
QhavboHDZQ4trjJ0q02ZwWphFRvN2nzekpys3rSYXxhFCUM3tJlq8D1YvTzk7WRP
Kz98Mxa/K8LXplKq99JS4GFyGVZFtDX3XG4kTpAeKlVxSrYpACFgR9tTD2CDVALf
t1nosvRSE/ZsIinTag2+/gMSiE5NBGR+ocDcFVaGEdDtw3WSn513do2lVihpi2uy
vwYHcbqDDFN7afjlD8FOb5lJgCmLs8pc+2r52mKx3g5w28wgqvkihpQHRZDRsIM=
=UYFM
Reply all
Reply to author
Forward
0 new messages