[erlang-questions] about fun vs. functions schisofrenia

Andras Georgy Bekes <>
Fri Jan 11 12:25:32 CET 2008


I'm not an expert in the topic, but I think I can answer the questions.
Correct me if I'm wrong.

> What do you think about artificial discrimination between "funs" and
> functions in Erlang?
There is an important difference:

If you have a fun from a module that is purged, the fun is invalidated 
and therefore your process is killed. However, having a module name and 
function name is perfectly valid, no matter how many times you upgrade 
the module, you can call the function any time (if it exists in the 
current version of the module).

> You
> have to define separate functions that requre "funs" and a separate
> set of functions that require funs.
Higher order functions should work with fun's.
If you want to store a reference to a function in the long term 
(surviving module upgrades), store the module and function name. When 
you want to use it as the argument of a higher-order function, use the 
expression
fun Module:Function/Arity
to "convert" your function reference to a fun.

> This breaks the code at every 
> corner (or export everything but this is nonsense).
>
> How should I call
>
> 	PrintedEntities = rpc:pmap( {?MODULE, print_entity},
>                                     [AttributeNames, ExportFunctions,
> State], SortedEntities ),
> without being forced to export the `print_entity/4' function? I would 
> really like to keep it "private".

This is sad, but it is a problem of this particular higher-order 
function, not the Erlang language. Unfortunately, there are many 
higher-order functions accepting funs and many accepting 
module+function name :-(

	Georgy



More information about the erlang-questions mailing list