Вопрос

I have the code below with this error:

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]).

I know its empty but still I figured it would show 0.

Это было полезно?

Решение

you didn't pass any variable to orddict:size().

state.clients returns an integer -- number of field 'clients' in the record state.

Другие советы

I figured it out!

-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]).

To run:

19> c(clients_size).
{ok,clients_size}
20> X = clients_size:init().
{state,[]}
21> clients_size:size(X).   
reference:size 0 
 ok
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top