[erlang-questions] dialyzer: user-defined types just synonyms?

Dmitry Belyaev <>
Thu Apr 22 16:16:01 CEST 2010


I thought type specs are made for external typechecks. Ok. Now I have 
this code:
-module(test).

-export([start/0]).

-spec start() -> ok.
start() ->
    AtomBad = get_atom(1),
    AtomGood = get_atom(2),
    check_atom(AtomBad),
    case check_atom(AtomGood) of
        ok -> ok;
        error -> error
    end.

-spec get_atom(any()) -> good | bad.
get_atom(1) ->
    bad;
get_atom(_) ->
    good.

-spec check_atom(good | nice) -> ok | error.
check_atom(Atom) ->
    ok.

It doesn't warn me about check_atom(AtomBad).

Tobias Lindahl wrote:
> 2010/4/22 Dmitry Belyaev <>:
>   
>> And one more question. I have a code:
>> -module(test).
>>
>> -export([start/0]).
>>
>> -spec start() -> ok.
>> start() ->
>>   %%check_atom(bad),
>>   case check_atom(good) of
>>       ok -> ok;
>>       error -> error
>>   end.
>>
>> -spec check_atom(good | nice) -> ok | error.
>> check_atom(Atom) ->
>>   ok.
>>
>> Dialyzer shows:
>> test.erl:10: The pattern 'error' can never match the type 'ok'
>> I'd like it to pay more attention to -spec than to function's body so I
>> wouldn't get this message. Is it possible?
>>     
>
> Once again, no. You cannot widen a success typing of a function using
> a spec. The type of the function will be the intersection of the type
> in the spec and whatever type Dialyzer infers for the function.  In
> this case it finds that the error clause is unreachable since the
> function unconditionally returns ok.
>
> Tobias
>
>   



More information about the erlang-questions mailing list