Use fun M:F/A, where M is a module name, F is a function name and A is arity.
M and F are atoms, A is non_neg_integer,
17> A = 1.
1
18> X = fun lists:reverse/A.
#Fun<lists.reverse.1>
19> X([1,2,3]).
[3,2,1]
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
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_()
On Monday, June 18, 2012, Samuel wrote:
> > 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).
no, this is exactly what I wanted to do. I have a function name as an atom
in a variable, and I needed a Fun reference out of it.
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.
no, believe it or not but I actually may know the difference between an
atom and a function. no matter how incredible this may sound.