[erlang-questions] gb_trees, strange behaviour

Michal D. <>
Fri Nov 5 18:35:26 CET 2010


> Michal D. wrote:
>>
>> Hi again,
>>
>> I'm having a lot of problems starting out with Erlang. It appears to me
>> that
>> I can't update a specific key of a gb_tree, while the other ones work
>> fine.
>> Can anyone explain the following behaviour?
>>
>>> X = gb_trees:from_orddict([{trace,false},{limit,-1},{timeout,-1}]).
>>
>> {3,{limit,-1,{trace,false,nil,nil},{timeout,-1,nil,nil}}}
>>>
>>> gb_trees:update(limit,true,X).
>>
>> {3,{limit,true,{trace,false,nil,nil},{timeout,-1,nil,nil}}}
>>>
>>> gb_trees:update(timeout,true,X).
>>
>> {3,{limit,-1,{trace,false,nil,nil},{timeout,true,nil,nil}}}
>>>
>>> gb_trees:update(trace,true,X).
>>
>> ** exception error: no function clause matching
>>                    gb_trees:update_1(trace,true,nil)
>>     in function  gb_trees:update_1/3
>>     in call from gb_trees:update/3
>
> The update calls that you think they work, work by fluke. The problem is
> that even though you may think you have created a gb tree, but in fact you
> have not.
>
> The problem lies in your first call:
>
>  X = gb_trees:from_orddict([{trace,false},{limit,-1},{timeout,-1}]).
>
> You promised you will supply an orddict, but in fact you have not.
> Use:
>
>  X =
> gb_trees:from_orddict(orddict:from_list([{trace,false},{limit,-1},{timeout,-1}])).
>
> instead and things will work as expected.
>
> Kostis
>

Thank you for the analysis!

Maybe we should update the documentation? It's pretty common to use a
list of tuples as a dictionary in other languages and that's what
appears to be specified in the Erlang documentation:

----------
from_orddict(List) -> Tree

Types:

List = [{Key, Val}]
 Key = Val = term()
Tree = gb_tree()

Turns an ordered list List of key-value tuples into a tree. The list
must not contain duplicate keys.
----------

Although on a second reading, it seems that the word "orderered" in
"ordered list" is key. So is an orddict just an ordered list of
tuples? But then again, why would it have to_list/1 if it was just a
list?

Cheers,

Michal


More information about the erlang-questions mailing list