[erlang-questions] why can't _ be passed around?

Pierpaolo Bernardi olopierpa@REDACTED
Thu Sep 22 14:46:00 CEST 2011


On Thu, Sep 22, 2011 at 14:08, Matthias Lang <matthias@REDACTED> wrote:
> Hi,
>
> Idle curiosity: why can't I pass or send _?
>
> I can write:
>
>  f() ->
>    receive _ -> ok
>    after 500 -> exit(timeout) end.
>
> But I can't write
>
>  g(Pattern) ->
>    receive Pattern -> ok
>    after 500 -> exit(timeout) end.
>
>  f() ->
>    g(_).
>
> Nor can I write
>
>  f() ->
>    g({my_atom, 3, _}).
>
> Ok, I realise I'm not allowed to because _ isn't bound. But _ can't
> ever be bound (right? or is there another way of thinking about _?).

Pattern matching is syntax: it is just a very concise way to write a
fixed (at compile time) cascade of nested case's.

> I've written g/1 as a macro. There doesn't seem to be a catch.

so, yes, macros would be the right tool, if Erlang had good macros.

> Thoughts? Am I asking for patterns to be first class?

It looks so to me.



More information about the erlang-questions mailing list