Erlang and records

What is wrong with this code? I was expecting "titi"in person.name, but I still have one "toto"! More specifically, how to change an entry in a function?

init1()->
    S=#person{name="toto"},   %record creation and field setting
    fct(S),
    io:format("~s~n",[S#person.name]).

fct(R)->
    R#person{name="titi"}.    %record updating
+3
source share
4 answers
Berto, I think you are a little ahead of yourself. You really need to understand the basics of immutability before writing code. (i.e., the "variables" do not change: you can only assign one value to them.) I suggest you read the free online guide, Learn You Some Erlang For Great Good, http://learnyousomeerlang.com/ . The section that covers the basics of variables is http://learnyousomeerlang.com/starting-out-for-real#invariable-variables .
+7
source

fct():

init1()->
    S=#person{name="toto"},   %record creation and field setting
    S2 = fct(S),   % Get updated record
    io:format("~s~n",[S2#person.name]).

fct(R)->
    R#person{name="titi"}.    %record updating
+10

, . , - , , @hdima. , . Erlang, . ETS, , .

, ETS, dataโ€‹โ€‹strong > , /. , / :

  • ""
  • ,
  • "" /

Without writing new data to the dictionary / table, it will be lost, since your new data was.

+2
source

Inside fct (), you are not mutating the entry, but you are returning a new value for the entry, which must be used later. If you call fct (S) without referring to the return value, you will lose this new value ("titi").

0
source

Source: https://habr.com/ru/post/1784556/


All Articles