OrderedSet indexing

Any idea why OrderedSet indexing is deprecated? If I follow the recommendation then I would have to keep a counter and iterate until I find the ith item?

julia> using DataStructures

julia> os = OrderedSet([2,3,1,4,7,6])
OrderedSet{Int64}([2, 3, 1, 4, 7, 6])

julia> os[2]
┌ Warning: indexing is deprecated for OrderedSet, please rewrite your code to use iteration
│   caller = top-level scope at none:0
└ @ Core none:0
3

If you try

@edit os[2]

it will take you to the relevant method, above which you find a reference to

https://github.com/JuliaCollections/DataStructures.jl/pull/180#issuecomment-400269803

which explains the reason.

In case the code did not have a comment like this, use the Github “blame” functionality on source files:

https://github.com/JuliaCollections/OrderedCollections.jl/blame/master/src/ordered_set.jl

to find the PR/commit that changed something.

1 Like

Thanks.

And, I agree with this comment :slight_smile: