Matching fun M:F/A

Richard O'Keefe raoknz@REDACTED
Sun Jan 5 14:13:33 CET 2020


I'm trying to think of a non-*accidental* semantics for fun patterns.
fun _:_/n matches any function reference of arity n, which certainly
has something to do with how we can call it.
fun _:_/_ matches any function reference.
I'm struggling to imagine plausible use cases for this.
Do you have some examples?

On Wed, 1 Jan 2020 at 05:13, Pierre Fenoll <pierrefenoll@REDACTED> wrote:
>
> Hi,
>
> Since a few releases, the value fun M:F/A (provided M, F & A are bound) is a literal. It can be read with file:consult/1 as well as erlang:binary_to_term/1.
>
> Running OTP 22, funs can be compared:
>
> eq(F) ->
>     %% Compiles & works as expected.
>     F == fun lists:keysearch/3.
>
> However MFAs cannot be matched:
>
> %% syntax error before: 'fun'
> fmatch(fun lists:keysearch/3) -> true;
> fmatch(_) -> false.
>
> cmatch(F) ->
>     case F of
>         %% illegal pattern
>         fun lists:keysearch/3 -> true;
>         %% illegal guard expression
>         X when X == fun lists:keysearch/3 -> true;
>
>         %% illegal pattern
>         fun lists:_/3 -> inte;
>         fun _:handle_cast/2 -> resting;
>         _ -> false
>     end.
>
> Is this intended?
>
> I believe it would be interesting to allow such patterns as well as for fun _:_/_.
> This could help in optimization through specialization and probably would make for some new approaches.
> Among all funs only fully qualified funs can be expressed this way so this behaviour could be a bit surprising to some but MFAs are already comparable today so I'd say we're already halfway there.
>
> Thoughts?
>
> PS: it seems one is no longer able to log into bugs.erlang.org with GitHub credentials as https://bugs.erlang.org/login.jsp?os_destination=%2Fdefault.jsp doesn't provide the option anymore. Is this normal?
>



More information about the erlang-questions mailing list