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

38 views
Skip to first unread message

Roberto Ostinelli

unread,
Jun 17, 2012, 12:11:30 PM6/17/12
to Erlang
Dear list,

is it possible to reference a fun from its atom name? For instance:

1> eunit:test({generator, fun hello_test_/0}).

-> everything is ok.

2> FunName = 'hello_test_/0'.
'hello_test_/0'
3> eunit:test({generator, fun FunName}).
* 1: syntax error before: '}'

Any ideas?

Thank you,

r.

Michael Uvarov

unread,
Jun 17, 2012, 1:20:14 PM6/17/12
to Roberto Ostinelli, Erlang
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-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Michael Uvarov

unread,
Jun 17, 2012, 1:24:04 PM6/17/12
to Roberto Ostinelli, Erlang
F = hello_test_, Fun = fun ?MODULE:F/0.

Tilman Holschuh

unread,
Jun 17, 2012, 1:40:32 PM6/17/12
to Roberto Ostinelli, Erlang
Did you try?

fun() -> apply(?MODULE, hello_test_, []) end.

Cheers
- Tilman

Zabrane Mickael

unread,
Jun 17, 2012, 1:44:54 PM6/17/12
to Roberto Ostinelli, Erlang
fun M:F/A

Sent from my iPhone
_______________________________________________

Roberto Ostinelli

unread,
Jun 17, 2012, 3:12:03 PM6/17/12
to Michael Uvarov, Erlang
Thank you all, I ended up using this one.

r.

Samuel

unread,
Jun 18, 2012, 3:11:15 AM6/18/12
to Roberto Ostinelli, Erlang
> 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

Roberto Ostinelli

unread,
Jun 18, 2012, 11:04:48 AM6/18/12
to Samuel, Erlang, Roberto Ostinelli

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.

:)

...r.
Reply all
Reply to author
Forward
0 new messages