logo
down
shadow

Global variables in erlang


Global variables in erlang

By : ifateli
Date : November 19 2020, 01:01 AM
Any of those help Other ways are using ETS tables (simpler to use than Mnesia, but don't offer distribution, persistence or transactions), or using a gen_server process to store global values.
code :


Share : facebook icon twitter icon
In Erlang how is using registered processes different from global variables in a traditional language?

In Erlang how is using registered processes different from global variables in a traditional language?


By : rytis.usalis
Date : March 29 2020, 07:55 AM
it fixes the issue I frequently see examples using process registration to simplify the example code (by avoiding another function parameter). This has the effect that newbies tend to use process registration a whole lot (monkey see, monkey do).
And they (newbies) tend to change the registration as part of normal operations. They tend to construct atoms to register as, and have other processes construct atoms to look up processes. When you do this, then you are having shared state concurrency (limited, but still bad).
Erlang - Global Variables .. yes I know, I know

Erlang - Global Variables .. yes I know, I know


By : user2220076
Date : March 29 2020, 07:55 AM
seems to work fine I GOT IT!!
Wish the docs, would say this under the lookup function.. Better yet, everyone who writes tutorials on ets, or more so books
code :
TableID = ets:new(tb, [set,public,named_table])
Erlang, why does it not have global variables and records?

Erlang, why does it not have global variables and records?


By : he110world
Date : March 29 2020, 07:55 AM
Hope that helps Your question is lacking some context, it could be useful to have the whole module to give a good answer.
Anyway, the function you show is quite simple, it is a function that return a client state record with the field gui equal to GUIName.
code :
%% the record state definition, it will include
%% the client nickname,
%% the gui name (or pid ?)
%% the client cart with a default value equals to the empty list
-record(cl_st,{nick,gui,cart=[]}).

initial_state(Nick, GUIName) -> 
%% the role of this function could be to start processes such as the gui
%% and return the initial state
    %% add some code to start the gui
    #cl_st { gui = GUIName, nick = Nick}.

%% an example of an interface function to add some item to the client cart
%% it simply pack the parameters into a tuple and send it to the server
%% Note that the server is identified by its pid, so somwhere there must be a unique
%% server that keep the list of all clients and their server pid
add_to_cart(Pid,Item,Price,Quantity) ->
    Pid ! {add_to_cart,Item,Price,Quantity}.

%% this function calls the init function, starts the server in a new process (usage of spawn) with the
%% initial state and returns the server pid 
start(Nick,GUIName) ->
    State = initial_state(Nick, GUIName),
    spawn(?MODULE,cl_loop,[State]).

stop(Pid) ->
    Pid ! stop.

%% the server loop
%% this example manages 2 kind of messages
cl_loop(State) ->
    receive
        %% add to cart simply builds a tuple made of item, price and quantity and add it to a list
        %% of tuple representing the cart content.
        %% it calls itself recursively with a new state as parameter where the old cart
        %% is replaced by the new one
        {add_to_cart,Item,Price,Quantity} ->
            NewCart = [{Item,Price,Quantity}|State#cl_st.cart],
            cl_loop(State#cl_st{cart=NewCart});
        %% to stop the server, it calls the terminate callback function and does not call itself recursively 
        stop ->
            terminate(State);
        %% other messages are ignored, the server simply calls itself with an unchanged state
        Ignored ->
            cl_loop(State)
    end. 

%% a callback function to terminate the server properly
terminate(State#cl_st{gui = GUIName, nick = Nick}) ->
    %% some code to stop the gui
    {stopped_client, Nick}.
How do I declare global variables in Javascript and change global variables and call global variables?

How do I declare global variables in Javascript and change global variables and call global variables?


By : Cassady Steel
Date : March 29 2020, 07:55 AM
like below fixes the issue First, I declare global variables. Second, I change the variable's value in a function. Finally, I call the variable. , Your code is equivalent to this
code :
var a;
function a(){
    a=1;
}
a = 0;
console.log(a);
var a = 0;

function functionA(){
    a = 1;
}

functionA();
console.log(a);
How to create global variables in Erlang

How to create global variables in Erlang


By : Sleepydragn1
Date : March 29 2020, 07:55 AM
wish of those help The "easiest way" is to create a named ets table, and put it in there.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org