John Galt
John Galt

Reputation: 257

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

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?

Upvotes: 5

Views: 3511

Answers (2)

Zed
Zed

Reputation: 57658

Just an alternative which uses pattern matching:

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

Upvotes: 22

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

Upvotes: 8

Related Questions