Search code examples
erlangrecorderl

Adding to an existing value in Erlang


I am attempting to create a function that stores a number into a record and then adds value X to that number every time the function runs.

Value: 5
Run Function (Add One):   1
Value should be: 6
Run Function (Add One):   1
value should be 7

I tried to use a record:

-record(adder,{value :: integer()}).

---function 
   Number = random:uniform(6),
        L=#added{value = Number + #added.value}.

This does not work as it resets the value every time. Any suggestions?


Solution

  • Take a look at this code:

    -module(test).
    -export([add/1]).
    
    -record(adder, {value=6}).
    
    add(X) ->
        #adder{value = X + #adder.value}.
    

    If you compile this in your shell, any call to "add(3)" will result in "{adder,5}" and not in "{adder, 9}". Take a look:

    Eshell V6.4  (abort with ^G)
    1> c(test).
    {ok,test}
    2> test:add(3).
    {adder,5}
    3> test:add(3).
    {adder,5}
    

    How come? This is because records are actually tuples. The expression "#adder.value" in the last line is evaluated as the position of the field "value" in your adder tuple, which is 2. Let's have some proof. Change the definition of your record:

    -module(test).
    -export([add/1]).
    
    -record(adder, {field1, field2, value=6}).
    
    add(X) ->
        #adder{value = X + #adder.value}.
    

    Now, recompiling your code and calling add(3) again would result in

    1> c(test).
    {ok,test}
    2> test:add(3).
    {adder,undefined,undefined,7}
    

    I've asked myself, how you came up with this question. Didn't you want your function to be something like this?

    add2(#adder{value = V} = R, X) ->
        R#adder{value = V + X}.