[erlang-questions] Dialyzer and List Type

Stavros Aronis aronisstav@REDACTED
Tue Apr 19 11:10:18 CEST 2016


I would not phrase it like that... you are hopefully not writing
specifications just for the sake of testing or being shouted at by
Dialyzer! :-) If your function should work for the empty list, then your
specification should include it.

It is true, however, that with an "[atom(),...]" spec (non-empty list of
atoms), you will get an error, as this type has no values in common with
the "[integer()]" (possibly empty list of integers).

Stavros

On Tue, Apr 19, 2016 at 10:32 AM PhayTsukiming <moonsolo@REDACTED> wrote:

> Thanks for you explanation. One more question: should I use non empty list
> specification to make dialyzer complain?
>
>
> On Tue, Apr 19, 2016 at 4:03 PM Stavros Aronis <aronisstav@REDACTED>
> wrote:
>
>> 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/fa939fb4/attachment.htm>


More information about the erlang-questions mailing list