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

Robert Virding <>
Tue Apr 10 14:44:35 CEST 2012

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. 


----- Original Message -----

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) -> 
{noreply, State}; 

Any idea? 
erlang-questions mailing list 

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

More information about the erlang-questions mailing list