Arity of funs

Bjorn Gustavsson bjorn@REDACTED
Wed Mar 15 11:49:48 CET 2000


Your trick will only work on funs defined in the shell, not on funs defined
in an arbitrary module. Furthermore, it is highly dependent on exactly how
funs are represented internally in the shell.

BTW, I deliberately decided to not implement a way to return the arity
of a fun, because that information is stored in the fun table in the loaded
code, not in the fun representation itself. And there might be a fun, but
no loaded code for it.

/Bjorn

Luke Gorrie <luke@REDACTED> writes:

> 
> Bjorn Gustavsson <bjorn@REDACTED> 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
> 

-- 
Björn Gustavsson            Ericsson Utvecklings AB
bjorn@REDACTED      ÄT2/UAB/F/P
			    BOX 1505
+46 8 727 56 87 	    125 25 Älvsjö




More information about the erlang-questions mailing list