Search code examples
erlangsizerecordordereddictionary

Erlang: orddict:size() function for record?


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.


Solution

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

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