Problem when using anonymous functions inside erlang modules
I was working with anonymous functions in erlang when a problem caught my attention. function is defined as follows
-module(qt).
-export([ra/0]).
ra = fun() -> 4 end.
this however does not work
-export(Ra/0]). Ra = fun() -> 4 end.
and this too can anyone tell me why erlang exhibits this behavior?
+2
source to share
1 answer
Erlang module cannot export variables, only functions.
You can achieve something similar to exporting variables by exporting a function with zero arguments that simply returns a value (the anonymous function is a valid return value):
-module(qt).
-export([ra/0]).
ra() ->
fun() -> 4 end.
You can now use it from the shell:
1> c(qt).
{ok,qt}
2> qt:ra().
#Fun<qt.0.111535607>
3> (qt:ra())().
4
+5
source to share