[erlang-questions] Pass a pattern to a function

Jachym Holecek freza@REDACTED
Tue Feb 10 19:18:59 CET 2009


Hello,

# Sten Gruener 2009-02-10:
> I try to write a function to scan the message queue and discard matched
> messages.
> 
> Like this:
> 
> discard(Pattern) ->
>     receive
>         Pattern ->
>             discard(Pattern)
>     after 0 ->
>         ok
>     end.
> 
> the problem is, that I cannot call discard({_, {msg}}) since _ is a wildcard
> representation. Are there any solutions?

Patterns aren't first class objects so I'm afraid you're SOL. You could
implement your discard/1 as a parse transform, but under the hood that
would just create per-pattern-specialised discard functions. Alternatively,
it would be doable with a macro, but I can't imagine that not being ugly.

	-- Jachym



More information about the erlang-questions mailing list