[erlang-questions] Dialyzer and List Type
Stavros Aronis
aronisstav@REDACTED
Tue Apr 19 10:03:46 CEST 2016
Hi!
The most general type that foo can have is [number()] -> [number()]. The
spec restricts both number() instances to just integer(). Then, since bar's
only argument is given as is to foo, it should also be [integer()].
If you specify bar's argument as atom() then there is no way to make a
successful call to foo and Dialyzer complains. If however bar's argument is
a list of atoms, then there is a way, though a bit trivial, for a call to
succeed: an empty list is both a valid list of atoms and a valid list of
integers.
Dialyzer can see this, and does not complain. Perhaps a patch is needed,
for a warning of type "only the empty list satisfies the given spec"...
Cheers,
Stavros
On Tue, 19 Apr 2016, 08:50 PhayTsukiming, <moonsolo@REDACTED> wrote:
> Hi list, I am new to erlang and dialyzer. I have encounter a problem when
> I tried to fiddle with type specifications.
>
> Say, I have two functions:
>
> -module(t).
> -export([foo/1, bar/1]).
>
> -spec foo([integer()])->[integer()].
> foo(Arg)->
> [I+1||I<-Arg].
>
> -spec bar(atom())->[integer()].
> bar(_Arg)->
> foo(_Arg).
>
> Then apply dialyzer to this module and dialyzer warns:
>
> t.erl:8: Invalid type specification for function t:bar/1. The success
> typing is ([integer()]) -> [integer()]
>
> But if I change the specification of bar to:
>
> -spec bar([atom()])->[integer()].
>
> dialyzer will show no warnings. My intention is to make sure the argument
> of bar is the same type of list as the argument of foo.
>
> Is this normal? Do I miss or misunderstand something about success typing
> or dialyzer?
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160419/5b1cdb94/attachment.htm>
More information about the erlang-questions
mailing list