[erlang-questions] Passing functions as arguments requires export?

Michal D. michal.dobrogost@REDACTED
Wed Nov 3 18:47:44 CET 2010


Ah, thank you! This way doesn't require the incr/1 to be exported, as
expected (at least by me =)).

Michal

On Wed, Nov 3, 2010 at 2:25 PM, Kostis Sagonas <kostis@REDACTED> wrote:

> Michal D. wrote:
>
>> Hi All,
>>
>> I'm having some problems working with functions in Erlang.  Specifically,
>> passing functions as arguments. Given the following module:
>>
>> -module(test).
>> -export([res/0]).
>>
>> res() -> lists:map({test,incr}, [0,1,2]).
>>
>> incr(X) -> X + 1.
>>
>> I stumbled upon the syntax {test,incr} to pass a (not anonymous) function.
>> When I go to run this code in the emulator, I get an error that doesn't
>> get
>> resolved unless I add "incr/1" to the export list.  However, there's no
>> reason to export the function because I don't ever want to call it
>> directly.  What am I missing?
>>
>
> You are using a really obsolete language feature that should have been
> completely eliminated by now.  There is absolutely no reason for it to still
> exist in the language.
>
> Use the more modern (and more fun ;-) construct instead:
>
>  res() -> lists:map(fun incr/1, [0,1,2]).
>
> Kostis
>


More information about the erlang-questions mailing list