[erlang-questions] Variables and side-effects

Jeremy Ong <>
Sun Dec 30 20:48:23 CET 2012


Other possibilities for handling global variables:

Using app configs (either with the config file or application:set_env/2
application:get_env/3)
Defining a macro in a header file that you include in every module that
needs to access the constant
Returning the value in a function exported by some module (this seems
strange to me).

If the value is truly constant and is meaningful to a specific application,
I would strongly recommend the first approach. The second approach is a bit
harder to reason about and requires more resource juggling for the
developer.


On Sun, Dec 30, 2012 at 12:56 PM, Tyron Zerafa <>wrote:

> My idea is to serialize a function, send it over to a remote node, decode
> it there and execute it. I am trying to find the possible things that may
> go wrong in this approach.
>
> Since there is no global variables, then I only need to care for the
> process dictionary and ets tables.
> However, I am finding a LOT of operations with side effects such as
> mnesia, messages and dets which somehow need to be reflected on the
> original node.
>
> Any idea how I can go about this differently or whether I am missing
> anything?
>
>
> On Sun, Dec 30, 2012 at 4:49 PM, Ulf Wiger <> wrote:
>
>>
>> On 30 Dec 2012, at 12:16, Tyron Zerafa wrote:
>>
>> I have been reading about Erlang and I am finding a problem grasping the
>> meaning of some terms related to variables.
>>
>> What do global, free and unbound variables stand for in Erlang? Do they
>> mean the same thing?
>>
>>
>> Global variables is what Erlang doesn't have. ;-)
>>
>> You can simulate global variables using processes or (within the scope of
>> a process) using the process dictionary, as Michael Turner pointed out.
>>
>>
>> Is there a notion of global/free/unbound variables in Erlang? For
>> instance, can I somehow implement this lambda:  λy ->* *x+y+1 ?
>>
>>
>>
>> You can do that, by passing X to the function that creates the lambda:
>>
>> lambda(X) ->
>>    fun(Y) ->
>>          X + Y + 1
>>    end.
>>
>> The closure will include X in its environment. Of course X will be
>> immutable.
>>
>> If Erlang does not support global/ free/unbound variables, can we say
>> that a function does not have side-effects?
>>
>>
>> You can't say that generally, since you can call side-effecting functions
>> from any function. There is currently no way to guarantee that a function
>> is pure, either through static or dynamic analysis.
>>
>> OTOH, the number of side-effecting operations in Erlang are relatively
>> few, so it's not too hard to determine that a function is pure in most
>> cases. Note that any function that calls other modules will have to be
>> treated as potentially side-effecting, since the semantics of the remote
>> call is determined at the time of the call, and dynamic code loading can
>> completely alter the type signature and semantics of an exported function.
>>
>> BTW, variables are free until bound, but you cannot reference an unbound
>> variable unless that expression also binds a value to it. In other words,
>> an unbound variable cannot appear in the RHS of an expression. Thus, you
>> cannot get a null reference exception in Erlang, which even Sir Anthony
>> Hoare will agree is a Very Good Thing. [1]
>>
>> BR,
>> Ulf W
>>
>> [1]
>> http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
>>
>>
>>
>>  Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
>> http://feuerlabs.com
>>
>>
>>
>>
>
>
> --
> Best Regards,
> Tyron Zerafa
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121230/bf1886a1/attachment.html>


More information about the erlang-questions mailing list