JuliaCollections / JuliaCollections/DataStructures.jl

Set Empty DefaultDict to new struct object may leads to pass by copy

Open
#793 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
745
Forks
261
PR merge metrics
No merged PRs in 30d

Description

Based on my tests using the DataStructures package, if you create a new DefaultDict empty and pass it to your new object it does not pass by reference but by copy and this leads to strange behaviour (imho). This is very subtle for me and should give some error or warning when you try.

SIMULATE THE ISSUE
1. Create Empty DefaultDict
julia> using DataStructures

julia> a = DefaultDict(0)
DefaultDict{Any, Any, Int64}()
2. Create your struct as dict
julia> struct D1
       data::Dict
       end
3. Assign the default dict to your object
julia> d = D1(a)
D1(Dict{Any, Any}())
4. Update initial default dict
julia> a["A"] = 1
1
5. Check the object and you will see that it was not updated as expected
julia> d.data
Dict{Any, Any}()
HOW TO AVOID THIS BEHAVIOUR

Basically in the data struct instead of using a Dict object use AbstractDict and the issue will not happen. But I think for me this creates some inconsistency in the code. What we should do?

julia> using DataStructures

julia> a = DefaultDict(0)
DefaultDict{Any, Any, Int64}()

julia> struct D2
       data::AbstractDict
       end

julia> d = D2(a)
D2(DefaultDict{Any, Any, Int64}())

julia> a["A"] = 1
1

julia> d.data
DefaultDict{Any, Any, Int64} with 1 entry:
  "A" => 1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Julia REPL examples with DataStructures.DefaultDict, then inspect how DefaultDict is converted when passed to fields typed as Dict and AbstractDict. Compare the observed behavior with Julia's type-conversion rules; done should mean the expected behavior and an appropriate regression test or documentation change are established.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.