[erlang-questions] Dialyzer and erlang:error/1
Tobias Lindahl
tobias.lindahl@REDACTED
Wed Jul 8 15:41:32 CEST 2009
Oscar Hellström wrote:
> Hi All,
>
> I'm trying to express that a valid list of options will return ok while
> any invalid option would eventually throw an exception, but Dialyzer and
> I don't get along.
>
> Dialyzer says:
> foo.erl:14: The call foo:verify(Options::any(),[any(),...]) will never
> return since it differs in argument position 2 from the success typing
> arguments: (['bar' | 'foo'],[])
>
> This is entirely true, but it's not an error, it's what I want! I guess
> I'm failing to express what I want here, so could anybody help me?
>
I see what you are trying to do, but unfortunately it does not work this
way. The no_return() will disappear when you make the union.
What you need to do, although I admit that it is a bit constructed, is
to break out the clause that throws the exception to a separate
function. In this way the no_return() allows you to accept the exception
clause.
(Code below. Should work. Haven't tried it)
Best regards,
Tobias
-module(foo).
-export([verify/1]).
-spec verify([atom()]) -> ok.
verify(Options) ->
verify(Options, []).
-spec verify([atom()], [atom()]) -> ok.
verify([foo | Options], Errors) ->
verify(Options, Errors);
verify([bar | Options], Errors) ->
verify(Options, Errors);
verify([Option | Options], Errors) ->
verify(Options, [Option | Errors]);
verify([], []) ->
ok;
verify([], Errors) ->
bad_options(Errors).
-spec bad_options([atom()]) -> no_return().
bad_options(Errors) ->
erlang:error({bad_options, Errors}).
> Best regards
>
>
>
> ------------------------------------------------------------------------
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
More information about the erlang-questions
mailing list