Matching fun M:F/A

Vyacheslav Levytskyy v.levytskyy@REDACTED
Wed Jan 1 16:26:03 CET 2020


Hello,

you can match MFA like the following:

test(Pred) ->
     test1(Pred, fun lists:keysearch/3).

test1(F, F) ->
     match;

test1(_, _) ->
     nomatch.

Erlang shell:

3> tonic_test:test(fun lists:keysearch/3).
match
4> tonic_test:test(fun lists:member/2).
nomatch

It looks like function expression is not a pattern expression in the 
grammar (pat_expr), and erl_lint identifies it eventually as an illegal 
pattern.

Best regards,
Vyacheslav


On 31.12.2019 17:12, Pierre Fenoll 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 
> <http://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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200101/30f6e558/attachment.htm>


More information about the erlang-questions mailing list