Funs with variable arity?

Manfred Widera <>
Wed Jul 28 10:55:08 CEST 2004

Hi Erlang Experts, 

does anybody know a way of writing a function that generates funs of 
variable arity, e.g.

fun_generator(Code, Arity) -> ...

such that the following code fragments both work

F1 = fun_generator(Something, 1),
F5 = fun_generatot(SomethingElse, 5),
F5(A1, A2, A3, A4, A5).

One brute force (and very ugly) method is to enumerate all needed arities 
up to a sufficient threshold, e.g.

fun_generator(Code, 0) -> fun() -> ... end;
fun_generator(Code, 1) -> fun(A1) -> ... end;
fun_generator(Code, 2) -> fun(A1, A2) -> ... end;
fun_generator(Code, 42) -> fun(A1, ..., A42) -> ... end.

Is there a better solution?

It is not sufficient for me to have a fun of arity 1, and to provide a 
list of the arguments, because I want to inspect parse results of funs, 
and to return a fun that behaves identically in the runtime system 
(especially regarding the arity).


More information about the erlang-questions mailing list