Adding to an existing value in Erlang

最后都变了- 提交于 2019-12-02 03:02:08

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}.
-record(adder, {value = 5}).

add(Value) ->
    add(#adder{}, Value).

add(#adder{value =V} = Adder, Value) ->
    Adder#adder{value = V + Value}.

test() ->
  R1 = add(1),
  io:format("~p~n", [R1]),
  R2 = add(R1, 10),
  io:format("~p~n", [R2]).

Here is the output of running test:test().

6> c(test).
{ok,test}
7> test:test().
{adder,6}
{adder,16}
ok
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!