JuliaCollections / DataStructures.jl

Julia implementation of Data structures
https://juliacollections.github.io/DataStructures.jl/latest/
MIT License
689 stars 245 forks source link

no way to mutate contents of stack (and presumably other containers) #100

Closed andrewcooke closed 6 years ago

andrewcooke commented 9 years ago

this should illustrate the issue:

julia> s = Stack(Int)
DataStructures.Stack{DataStructures.Deque{Int64}}(Deque [Int64[]])

julia> push!(s, 0)
DataStructures.Stack{DataStructures.Deque{Int64}}(Deque [[0]])

julia> top(s) += 1
ERROR: syntax: invalid assignment location

julia> a = Array(Int, 0)
0-element Array{Int64,1}

julia> push!(a, 1)
1-element Array{Int64,1}:
 1

julia> a[end] += 1
2

julia> a
1-element Array{Int64,1}:
 2

not pressing - going to switch to arrays instead of stacks.

oxinabox commented 6 years ago

I don't think this is actually possible. If julia had have evolved in a different direction in 2015 it might have been. But right now primitive types are immutable, and thus functions whos return values are immuatble, can not be used as targets to mutate their source.

If a mutable value is used then it is fine:

julia> t = Stack(Ref{Int})
DataStructures.Stack{Ref{Int64}}(Deque [Ref{Int64}[]])

julia> b=Ref(1)
Base.RefValue{Int64}(1)

julia> push!(t,b)
DataStructures.Stack{Ref{Int64}}(Deque [Ref{Int64}[Base.RefValue{Int64}(1)]])

julia> top(t)[]+=3
4

julia> top(t)
Base.RefValue{Int64}(4)

I guess one could overload setindex for Dequeues, but right now we don't even overload getindex.

oxinabox commented 6 years ago

Closing for now. if it comes up, feel free to open a new issue for setindex or getindex I am undecided if we should have them