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

Pablo Vieytes pablo.vb80@REDACTED
Tue Apr 10 16:25:49 CEST 2012


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.

2012/4/10 Robert Virding <robert.virding@REDACTED>

> As Attila has said the way to do this:
>
> handle_cast(#inputmsg{}=Msg, State) ->
>     ... ;
>
> The reason your example doesn't work is that #inputmsg{_='_'} expands so
> that all the fields will get the value of the ATOM '_' which is definitely
> not the same as the VARIABLE _. In this case there is actually no need to
> do anything as the compiler automatically puts the variable _ for all
> fields which aren't explicitly mentioned.
>
> The main use of the _='_' construct is when you want to create a match
> pattern for ets/mnesia which is tuple which is INTEPRETED as a pattern.
> Then it is useful to have a construct which sets all unmentioned fields to
> the value of the atom '_' which is interpreted as the don't care variable.
>
> It is irrelevant whether you write #inputmsg{}=Msg or Msg=#inputmsg{},
> they are equivalent. I prefer the former, but many prefer the latter.
>
> Robert
>
> ------------------------------
>
> Hi,
> I'm trying to use records in functions pattern matching but I'm not
> able. I want something like that:
>
> handle_cast(Msg = #inputmsg{_='_'}, State) ->
>    do_something(Msg),
>     {noreply, State};
>
> Any idea?
> thanks.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120410/a8fc9639/attachment.htm>


More information about the erlang-questions mailing list