[erlang-questions] Enforcing a function spec in Dialyzer

Kostis Sagonas kostis@REDACTED
Thu Jun 17 22:41:14 CEST 2010


Zoltan Lajos Kis wrote:
> Hi all,
> 
> Is it possible to enforce Dialyzer to use the spec I provide for a function
> instead of deducting the specification from the function body?
> 
> For example in the example below I would like to force my spec on the 
> data() function,
> making Dialyzer stop issuing warnings about lists:keyfind always 
> returning false,
> get(Key) always returning 'undefined', and so on...

You cannot fool dialyzer so easily.  Whenever, it infers that the spec 
you have given is not OK, it continues the analysis with the inferred 
information rather than the spec.

The only way to achieve what you want is to use calls to some 
yet-to-be-implemented module and not include that module in the analysis 
as in:

-spec data() -> [{atom(), any()}].
data() -> not_yet_implemented:data().

%% Module not_yet_implemented looks as follows:
-module(not_yet_implemented).
-export([data/1]).

data() -> [].

%% so that it can be used for testing, but is not given to dialyzer.

> (The rational behind the question is that I rebuild the module every now 
> and then,
> so calling the data() function will actually return a list of key-value 
> pairs later.)
> 
> Thank you,
> Zoltan.
> 
> ----------------------
> 
> -spec( get(atom()) -> any() ).
> get(Key) ->
>     case lists:keyfind(Key, 1, data()) of
>         {Key, Val} -> Val;
>         false -> 'undefined'
>     end.
> 
> -spec( data() -> [{atom(), any()}] ).
> data() -> [].


More information about the erlang-questions mailing list