Arity of funs

Luke Gorrie <>
Wed Mar 15 11:36:07 CET 2000


Bjorn Gustavsson <> writes:

> There is no way to find out the arity of a fun.
> 
> There are, however, erlang:fun_info/1,2 BIFs that return some information
> about a fun, but not the arity. (These BIFs are meant to be used for
> debugging purposes, not for use in applications.)

Here's some horrible code for finding the arity of a fun. I don't know
if it works on all versions of Erlang, depend what fun_info returns. I
know it's ugly, but saying there is "no way" is provoking an ugly hack
anyway :-)

21> ArgCounter = fun({env, [_, [{clause,_,Args,_,_}|_]|_]}) -> length(Args) end.
#Fun<erl_eval.19.120858100>
22> ArityFinder = fun(F) -> ArgCounter(erlang:fun_info(F, env)) end.
#Fun<erl_eval.19.120858100>
23> ArityFinder(ArityFinder).
1
24> ArityFinder(fun(X, 1, {2, Y}) -> true end).
3




More information about the erlang-questions mailing list