[erlang-questions] specs for fixed length lists

Tony Rogvall <>
Sun Jul 8 19:47:16 CEST 2012


While thinking about this problem I wrote the following spec:

-spec get_buttons() ->
     {ok,{boolean(),{boolean(),{boolean(),{boolean(),[]}}}}}.

get_buttons() ->
    {ok,{true,{true,{false,12}}}}.

This passed dialyzer!

The reason (I suppose) is that the analysis is limited to depth = 2 (or 3).
Running typer while commenting out the spec gives:

-spec get_buttons() -> {'ok',{'true',{'true',{_,_}}}}.

I guess this is the real reason why dialyzer can not handle fixed size lists (at least not a list with a length greater than 3)

/Tony

On 7 jul 2012, at 21:53, Richard Carlsson wrote:

> On 2012-07-07 13:57, Tony Rogvall wrote:
>> Try this:
>> 
>>     -type buttons() :: [boolean() | [boolean() | [boolean() |
>> [boolean() | []]]]].
> 
> You certainly can _write_ it like that, just like you can write "1+1" instead of "2". To a tool like Dialyzer, it will still just be interpreted as a (nonempty) list of boolean(). The exact number of elements is not tracked. And if you for example try to say "[atom() | [integer() | []]]", it will just be the same as saying "nonempty list of atom()|integer()" - the order of occurrence of the element types is also not part of the list type.
> 
>    /Richard
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

"Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120708/78d91d32/attachment.html>


More information about the erlang-questions mailing list