Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: Is there a way to pattern match a record in a receive clause?

Tags:

erlang

I want to do a selective receive where a record property needs to be matched, but whatever syntax I try, I get an "illegal pattern" message.

loop(State) ->
  receive
    {response, State#s.reference} -> do_something()
  end.

Is this not possible?

like image 875
John Galt Avatar asked Oct 08 '09 03:10

John Galt


2 Answers

Just an alternative which uses pattern matching:

loop(#s{reference = Reference} = State) ->
  receive
    {response, Reference} ->
      do_something()
  end.
like image 139
Zed Avatar answered Oct 20 '22 08:10

Zed


loop(State) ->
    receive
       {response, R} when R =:= State#s.reference ->
             do_something()
    end.
like image 43
Maxim 'Zert' Treskin Avatar answered Oct 20 '22 07:10

Maxim 'Zert' Treskin