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

David Sveningsson ext@REDACTED
Wed Mar 25 16:31:06 CET 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Magnus Henoch wrote:
> David Sveningsson <ext@REDACTED> 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'.

Right, I meant {error, atom()}. I did however have a similar issue in my
code.

>> 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()}.

Lets say that foo looks like this:

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

I get the same error. It is true that the function will only return ok,
but I would like to reserve that it may return {error, atom()} in the
future. It that possible?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAknKTjoACgkQ6pa1H/H5pqWrKwCglz8a5i7qEG7DvLUq+fT1ThP7
mroAn3fgvCiIHilB6WJqulrmQaqCkGZD
=dHTW
-----END PGP SIGNATURE-----



More information about the erlang-questions mailing list