Wednesday, 16 August 2017

Julia dictionaries

Something unexpected happened today with Julia 0.5 dictionaries:

In []: test = Dict(1 => Dict(2 => 3))
       test[4]  = test[1]
       test[1][2] = 5
       test
Out[]: Dict{Int64,Dict{Int64,Int64}} with 2 entries:
       4 => Dict(2=>5)
       1 => Dict(2=>5)
So that looks like reference passing. Changing the original also changes the copy. But,

In []: test = Dict(1 => 2)
       test[3] = test[1]
       test[3] = 4
       test
Out[]: Dict{Int64,Int64} with 2 entries:
       3 => 4
       1 => 2
In the case above adjusting the value in the copy does not affect the value in the original. Maybe the direction of propagation is important:

In []: test = Dict(1 => 2)
       test[3] = test[1]
       test[1] = 4
       test

Out[]: Dict{Int64,Int64} with 2 entries:
       3 => 2
       1 => 4
Nope! Direction did not change the outcome. The dictionary of dictionaries appears to use references but the dictionary of keys and values does not. :|

No comments:

Post a Comment