Erlang:orddict:size()函数用于记录?

时间:2013-03-10 16:28:38

标签: erlang size record ordereddictionary

我的代码如下:

10> clients_size:init().
{state,[]}

11> clients_size:size().
** exception error: bad argument
     in function  length/1
        called as length(2)
     in call from orddict:size/1 (orddict.erl, line 65)
     in call from clients_size:size/0 (clients_size.erl, line 10)

clients_size.erl

-module(clients_size).
-export([init/0, size/0]).
-record(state, {clients}). 

init() ->
    #state{clients=orddict:new()}.


size()->
    Size_of = orddict:size(#state.clients),
    io:format("size ~p ~n ",[Size_of]).

我知道它是空的,但我仍然认为它会显示为0.

2 个答案:

答案 0 :(得分:4)

你没有将任何变量传递给orddict:size()。

state.clients返回一个整数 - 处于记录状态的字段“clients”。

答案 1 :(得分:0)

我明白了!

-module(clients_size).
-export([init/0, size/1]).
-record(state, {clients}). 

init() ->
    #state{clients=orddict:new()}.


size(S)->
    Size_of = orddict:size(S#state.clients),
    io:format("reference:size ~p ~n ",[Size_of]).

运行:

19> c(clients_size).
{ok,clients_size}
20> X = clients_size:init().
{state,[]}
21> clients_size:size(X).   
reference:size 0 
 ok