Storing a dictionary in a dictionary in Erlang
I have a dictionary which I use to store another dictionary using the name of a parameter.
I get a right hand side mismatch error.
here is my code
handle_cast({setState, Id}, State) ->
Id0 = dict:new(),
DQueue = queue:new(),
UQueue = queue:new(),
Id1 = dict:store(dQueue, [DQueue], Id0),
Id2 = dict:store(uQueue, [UQueue], Id1),
Id3 = dict:store(dSpeed, [], Id2),
Id4 = dict:store(uSpeed, [], Id3),
D = dict:store(Id, [Id4], State),
State = D,
{noreply, State};
Im not sure where the 开发者_运维技巧error comes from. I thought it might be because I store Id as the key in the main dictionary with the new internal dictionary as the value.
I need the name of the internal dictionary to be the value of the Id as there will be many of them and I need to access them by Id later.
Am I setting the dictionary up correctly? Does erlang allow dictionaries to hold dictionaries?
Thanks
Without trying the code, my bet is that you badmatch when doing State = D
given that State
is already bound in the function's head. On top of this, USpeed
and DSpeed
should be undefined unless you copy/pasted your function wrong.
How about a rewrite:
handle_cast({setState, Id}, State) ->
D = dict:from_list([{dQueue, [queue:new()]},
{uQueue, [queue:new()]},
{dSpeed, []},
{uSpeed, []}],
{noreply, D};
Which is simpler to read, avoids naming trouble and is about the same speed.
精彩评论