[erlang-questions] Fwd: Newbie question: avoiding massive 'case' statement
Vlad Dumitrescu
vladdu55@REDACTED
Wed Mar 5 22:16:19 CET 2008
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
More information about the erlang-questions
mailing list