[erlang-questions] pattern matching with records in a function

Robert Raschke <>
Tue Apr 10 16:46:22 CEST 2012


On Tue, Apr 10, 2012 at 3:25 PM, Pablo Vieytes <> wrote:

> thanks,
> now it works.
>
>
> >>Although I'm not quite sure what you want to achieve...
>
> I didn't write all code I needed.
>
> I needed something like this:
>
>
> -record(inputmsg, {action, data1, ..., datan}).
>
> handle_cast(#inputmsg{action=a}=Msg, State) ->
>      action_a(Msg),
>     {noreply, State};
>
>
> handle_cast(#inputmsg{action=b}=Msg, State) ->
>      action_b(Msg),
>     {noreply, State};
>
> handle_cast(#inputmsg{}=Msg, State) ->
>      action_default(Msg),
>     {noreply, State};
>
>
>
> and I needed data1...datan in action_x().
>
>
> thanks.
>
>
You can simplify this ever so slightly:

handle_cast(#inputmsg{action=Action} = Msg, State) ->
    action(Action, Msg),
    {noreply, State}.

action(a, Msg) -> action_a(Msg);
action(b, Msg) -> action_b(Msg);
action(_, Msg) -> action_default(Msg).

Robby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120410/2cc12971/attachment.html>


More information about the erlang-questions mailing list