[erlang-questions] How do I elegantly check many conditions?

Adam Lindberg adam@REDACTED
Wed Mar 25 10:30:14 CET 2009


I'm sorry, but you aren't making much sense here... :-/

I still think you're misusing the badmatch exception.

Cheers,
Adam

----- "Hynek Vychodil" <vychodil.hynek@REDACTED> wrote:

> On Tue, Mar 24, 2009 at 9:24 PM, mats cronqvist <masse@REDACTED>
> wrote:
> 
> > Hynek Vychodil <vychodil.hynek@REDACTED> writes:
> >
> > > code
> > >
> > > ok = is_new_email1(Email).
> > >
> > > doesn't looks like same amount of code
> > >
> > > try is_new_email2(Email) catch throw:email_in_use -> email_in_use
> end.
> > >
> > > First one turns return value to exception, second one turns
> exception to
> > > return value. Second one seems more code, does not?
> >
> >   but the exception generated in example 1 is a worthless
> 'badmatch'.
> >
> 
> I can flexible choose which worth-full 'badmatch'  will be generated
> just
> using {ok, Email} = {is_new_email1(Email), Email}. It all up to me. It
> is
> meaning of word 'flexible'.
> 
> 
> > > Even code
> > >
> > > is_new_email1(Email) ->
> > >   case mnesia:dirty_read(email, Email) of
> > >     []    -> ok;
> > >     _ -> email_in_use
> > >   end.
> > >
> > > seems less than
> > >
> > > is_new_email2(Email) ->
> > >   case mnesia:dirty_read(email, Email) of
> > >     []    -> ok;
> > >     _ -> throw(email_in_use)
> > >   end.
> >
> >   I think it should be written like this;
> >
> > is_new_email2(Email) ->
> >  try [] = mnesia:dirty_read(email, Email)
> >  catch _:_ -> throw({email_in_use,Email})
> >  end.
> >
> >  This is how your example 1 above has to be written too, if it is
> to
> >  give a helpful error message.
> >
> 
> Yes, but what prevent me
> 
> is_new_email1(Email) ->
>   case mnesia:dirty_read(email, Email) of
>     [] -> ok;
>     _ -> {email_in_use,Email}
>   end.
> 
> and again, less code and more flexible. Each variant with throw can
> be
> written without throw in far flexible way.
> 
> 
> >  mats
> >
> 
> 
> 
> -- 
> --Hynek (Pichi) Vychodil
> 
> Analyze your data in minutes. Share your insights instantly. Thrill
> your
> boss.  Be a data hero!
> Try Good Data now for free: www.gooddata.com



More information about the erlang-questions mailing list