[erlang-questions] Dialyzer warning: variable can never match since previous clauses completely covered the type

Magnus Henoch <>
Wed Mar 25 14:45:53 CET 2009


David Sveningsson <> writes:

> -spec foo() -> ok | {error, reason}.

This is not what you meant to write.  You declare that foo returns
either the atom 'ok', or a tuple of two atoms, 'error' and 'reason'.

> foo() ->
> 	case random:uniform(10) of
> 		N when N > 5 -> ok;
> 		_ -> {error, foobar}
> 	end.

Dialyzer believes your spec, and finds that your code can not possibly
return {error, reason}.  Therefore, the only remaining possibility is
'ok'.

You could write the spec as {error, foobar}, {error, atom()} or {error,
Reason::atom()}.

-- 
Magnus Henoch, 
Erlang Training and Consulting
http://www.erlang-consulting.com/




More information about the erlang-questions mailing list