Reputation: 23
I want to be able to write a function which when its input is in some invalid state (let's say it's an integer, and invalid means -1), it receives messages with any kind of information, but when its input is valid, it only receives messages of the same kind as its input. As an example, this is what a possible solution might look like:
f(-1) ->
receive
...
{a, AnyInput} ->
% Do something
...
end
f(ValidInput) ->
receive
...
{a, ValidInput} ->
% Do something
...
end
The main concern here is duplicating code, as the receive contains a large amount of, otherwise, identical code (there are also many other messages types in the same receive).
Is there any coding pattern which could help me here?
I also have the freedom to set the invalid state as any value, including undef, if that would help.
Upvotes: 2
Views: 50
Reputation: 8340
f(Input) ->
receive
...
{a, AnyInput} when Input =:= -1 ->
% Do something
...
{a, Input} ->
% Do something
...
end
Upvotes: 2