[erlang-questions] How to reduce the repetitious code in create mnesia table?

Mazen Harake <>
Tue Mar 30 09:44:34 CEST 2010

Why are you case-ing everything?

I remember I've done something similar before and what I did was that I 
always pattern matched on expected results (I.e. _why_ would you expect 
create_table to fail when creating only 6 tables?) and never cared about 
the "{aborted, Reason}" part. _if_ you get {aborted, Reason} you will 
get a badmatch... this is good because you can use pattern matching to 
get the reason from that instead of using 6+ case-clauses.

   Options = [{attributes, record_info(fields, ig_master)}],
   {atomic, ok} = mnesia:create_table(ig_master, Options),
   error:{badmatch,{aborted,Reason}} ->
     {error, Reason}

On 30/03/2010 09:09, 钱晓明 wrote:
> Hi, I want to create many tables in mnesia, but I find the code is very long
> if check the return value of mnesia:create_table every time:
> case mnesia:create_table(ig_master, [{attributes, record_info(fields,
> ig_master)}]) of
>      {aborted, Reason} ->
>           error_logger:error_msg("Fail to create table ~p: ~p!", [ig_master,
> Reason]),
>           throw({error, create_table});
>      {atomic, ok} ->
>           ok
>   end,
> %% repetitious code to create other 6 tables.
> Is there any way I can do to reduce the repetitious code? I have
> tried defining a new function accepting the name of record to do the same
> thing above, but can not compile code because of record_info.
> By the way, I do not find the way to leave a function except using
> throw/exit. Is there any way I can use to just leave current function,
> replace the "throw" statement above?
> Thanks!




Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.


More information about the erlang-questions mailing list