Matching fun M:F/A

José Valim jose.valim@REDACTED
Sat Jan 4 22:18:59 CET 2020

> How do you feel about being able to pattern match on fun M:F/A?

I personally don't agree with the argument of being "inconsistent". The
pattern matching syntax has always been a subset of the language and with
specific semantics. For example: #{1 => 2} is a literal map but #{1 => 2}
literally in a pattern will also match #{1 => 2, 3 => 4}. I also consider
the fact "fun M:F/A" is a literal to be an implementation detail rather
than part of the language specification.

Finally, I think pattern matching on fun M:F/A would lead to bad coupling
(it couples to the function name). For example, imagine that I implement
some function called "my_module:my_fun/1" which has some specific behaviour
on "fun lists:keysearch/3". At first, a user of my API is calling it as

my_module:my_fun(fun lists:keysearch/3, List).

And imagine elsewhere the user has to use the same function, but the
indexes are zero-based, so they have to wrap it like this:

my_module:my_fun(fun(Key, N, AList) -> lists:keysearch(Key, N + 1, AList)
end, List).

Those two functions will now behave in different ways, which would be
extremely confusing. Sure, you can achieve this by doing the manual
comparison with == (as with everything else) but I don't think we should be
further encouraging this behaviour. I personally don't remember ever
needing to compare function references.

José Valim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the erlang-questions mailing list