[erlang-questions] 12B4 dialyzer problem 5

Kostis Sagonas <>
Wed Sep 17 16:56:54 CEST 2008


Anthony Shipman wrote:
> I had a -spec that read
> 
> -spec getHandlerModule(string()) -> {ok, Module::atom(), StrmID::integer} | 
> error.

You shouldn't have had ;-)

To avoid typos like that, which sadly are very common, we _strongly_ 
recommend that you follow the convention of writing atoms quoted in spec 
declarations.  I.e. that you write:

-spec getHandlerModule(string()) ->
	{'ok', Module::atom(), StrmID::integer()} | 'error'.

> This caused code like
> 
>     case getHandlerModule(HName) of
>     {ok, Mod, DefaultID} ->
> 	OptionMeta = Mod:optionMetadata(),
> 	checkForm(Sess, Arg, OptionMeta, createStream4(DefaultID, HName));
>     error ->
> 	returnError(text:get(invalidHandlerForm))
>     end.
> 
> to result in the dialyzer message
>     The pattern {'ok', Mod, DefaultID} can never match the type 'error'
> 
> I can see where my typo is but I can't relate it the error message. I would 
> expect that the pattern will always match and I'd get a type error further 
> down-stream.

You have a point, but that's not how it works.  The inferred success 
typing of getHandlerModule/1 gets filtered by the spec declaration, so 
the {'ok', atom(), integer()} part of the success typing is thrown away 
(because it is not part of the specification of the function) and the 
only return value that remains for this function is the atom 'error'.

Dialyzer has warning options which would provide more information in 
this case, but these options are off by default.  I believe the option 
-Wunderspecs would have given you more clues.

Kostis



More information about the erlang-questions mailing list