[erlang-questions] reference a fun from its atom name

Samuel <>
Mon Jun 18 09:11:15 CEST 2012


> Thank you all, I ended up using this one.
>
>> F = hello_test_, Fun = fun ?MODULE:F/0.

Note that this is not exactly what you were trying to do. I guess you
were trying to create a fun to call a local (possibly unexported
function). The construction above creates a fun that does a fully
qualified call (thus, the function must be exported).

Your problem was that instead of a fun you defined an atom:

2> FunName = 'hello_test_/0'.
'hello_test_/0'

Anything enclosed in '' is an atom. What you were trying to do is probably

2> FunName = fun hello_test_/0.

That's the valid syntax to define a fun, and is roughly (not exactly)
equivalent to
2> FunName = fun() -> hello_test() end.

However the first construct will fail in the shell, because you are in
the erl_eval module context there:

2> FunName = fun hello_test_/0.
** exception error: undefined function erl_eval:hello_test_/0

But it will work if you use it in a module that defines hello_test_()

Regards
-- 
Samuel



More information about the erlang-questions mailing list