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

ra = fun() -> 4 end. 


this however does not work

Ra = fun() -> 4 end. 


and this too can anyone tell me why erlang exhibits this behavior?


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):

ra() ->
    fun() -> 4 end.


You can now use it from the shell:

1> c(qt).
2> qt:ra().
3> (qt:ra())().




All Articles