[erlang-questions] Fwd: Newbie question: avoiding massive 'case' statement

Adam Lindberg adam@REDACTED
Thu Mar 6 10:49:30 CET 2008


Hi David!

Why not convert the string to the module name directly?
zabc = list_to_atom(string:to_lower("ZABC")).

In that case you'd only need a case to handle if the module is undefined:

Module = list_to_atom(string:to_lower(TransactionType)),
case code:ensure_loaded(Module) of
    {module, Module} ->
        Module:process_request(Request);
    {error, nofile} ->
        error_logger:info_msg("Unknown transaction type")
end,

Hope this gets you closer to a solution!

Cheers!
Adam

--
Adam Lindberg
http://www.erlang-consulting.com

On Wed, Mar 5, 2008 at 10:16 PM, Vlad Dumitrescu <vladdu55@REDACTED> wrote:

> Hi!
>
>
>  On Wed, Mar 5, 2008 at 9:59 PM, David Mitchell <monch1962@REDACTED>
> wrote:
>  >  I've got a block of code that looks like:
>  >  case TransactionType of
>  >   "ZABC" ->
>  >     zabc:process_request(Request);
>  >   "Z123" ->
>  >     z123:process_request(Request);
>  >   "YPQX" ->
>  >     ypqx:process_request(Request);
>  >  ...
>  >   TransactionType ->
>  >     error_logger:info_msg("Unknown transaction type")
>  >  end
>
>  You could use something like
>
>  get_handler("ZABC")  ->
>  zabc;
>  get_handler("Z123") ->
>  z123;
>  .......
>  get_handler(_) ->
>  error_logger:info_msg("Unknown transaction type"),
>  error
>
>  and in your code just call
>
>  Module = get_Handler(TransactionType),
>  case Module of
>  error ->
>    done;
>  _ ->
>    Module:process_request(Request);
>  end,
>
>  best regards,
>  Vlad
>
>  --
>  Some people see things that are and ask, Why?
>  Some people dream of things that never were and ask, Why not?
>  Some people have to go to work and don't have time for all that.
>  --- George Carlin
>
>
>
> --
> Some people see things that are and ask, Why?
> Some people dream of things that never were and ask, Why not?
> Some people have to go to work and don't have time for all that.
> --- George Carlin
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080306/207d9dd5/attachment.htm>


More information about the erlang-questions mailing list