OR construction in erlang ?

Vladimir Sekissov <>
Tue Apr 12 22:43:15 CEST 2005


mscandar> ok = test(Arg) orelse foo().
mscandar> 
mscandar> will either exit with {badarg, ok}, since the value of ok = ok is ok, or it will exit with 
mscandar> {badmatch, ...}

Yes, sorry. Must be simply:

...
test_function(Args) == ok orelse throw(error_in_test_function),
...

> olivier.sambourg> I know I can achieve the same with a catch and case syntax but nesting 
> olivier.sambourg> several tests gets ugly (or increases the number of functions in a 
> olivier.sambourg> module).

You can use something like:

case catch
  begin
    {ok, ok} = {ok, test1()},
    {ok, Res2} = test2(),
    test3() orelse exit(Error3),
    ....
  end of
    {ok, Good} ->
       Good;
    {'EXIT', Error} ->
       ...
end,

mscandar> Note that:
mscandar> 
mscandar> ok = test(Arg).
mscandar> 
mscandar> exits with a badmatch if test(Arg) doesn't return ok. (This is often good enough.)
mscandar> 
mscandar> The logical operators (or, and, xor, not, orelse, andalso) will exit with badarg if their 
mscandar> operands are other than the atoms 'true' or 'false'.
mscandar> 
mscandar> Thus,
mscandar> 
mscandar> ok = test(Arg) orelse foo().
mscandar> 
mscandar> will either exit with {badarg, ok}, since the value of ok = ok is ok, or it will exit with 
mscandar> {badmatch, ...}
mscandar> 
mscandar> Mark.
mscandar> 
mscandar> Vladimir Sekissov wrote:
mscandar> > Good day,
mscandar> > 
mscandar> > olivier.sambourg> ok = test_function(Args) or throw(error_in_test_function).
mscandar> > 
mscandar> > ok = test_function(Args) orelse throw(error_in_test_function).
mscandar> > 
mscandar> > Best Regards,
mscandar> > Vladimir Sekissov
mscandar> > 
mscandar> > olivier.sambourg> Hi everyone
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> Is there an equivalent to the PHP "or" construction ? I'd like to be 
mscandar> > olivier.sambourg> able to throw errors when a match fails, i.e. :
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> ok = test_function(Args) or throw(error_in_test_function).
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> I know I can achieve the same with a catch and case syntax but nesting 
mscandar> > olivier.sambourg> several tests gets ugly (or increases the number of functions in a 
mscandar> > olivier.sambourg> module).
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> Thanks :)
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> --
mscandar> > olivier.sambourg> Olivier
mscandar> > olivier.sambourg> 
mscandar> > olivier.sambourg> 
mscandar> > 
mscandar> 



More information about the erlang-questions mailing list