0

我有以下带有此错误的代码:

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。

4

2 回答 2

4

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

state.clients 返回一个整数——记录状态中字段“客户”的数量。

于 2013-03-10T17:46:45.940 回答
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
于 2013-03-10T18:03:02.177 回答