{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/19921","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/19921/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/19921/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/19921/events","html_url":"https://github.com/JuliaLang/julia/issues/19921","id":199383832,"number":19921,"title":"Deepcopy issue with Dict","user":{"login":"tkoolen","id":2585880,"avatar_url":"https://avatars.githubusercontent.com/u/2585880?v=3","gravatar_id":"","url":"https://api.github.com/users/tkoolen","html_url":"https://github.com/tkoolen","followers_url":"https://api.github.com/users/tkoolen/followers","following_url":"https://api.github.com/users/tkoolen/following{/other_user}","gists_url":"https://api.github.com/users/tkoolen/gists{/gist_id}","starred_url":"https://api.github.com/users/tkoolen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tkoolen/subscriptions","organizations_url":"https://api.github.com/users/tkoolen/orgs","repos_url":"https://api.github.com/users/tkoolen/repos","events_url":"https://api.github.com/users/tkoolen/events{/privacy}","received_events_url":"https://api.github.com/users/tkoolen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-01-07T20:40:37Z","updated_at":"2017-01-08T20:24:17Z","closed_at":null,"body":"I'm getting some unexpected results using `deepcopy` for `Dict`. Consider the following code:\r\n\r\n```julia\r\ntype Foo\r\n   a::String\r\nend\r\n\r\ntype Bar\r\n    foo::Foo\r\n    fooDict::Dict{Foo, Int64}\r\nend\r\n\r\nfoo = Foo(\"foo\")\r\nbar = Bar(foo, Dict(foo => 3))\r\nbar2 = deepcopy(bar)\r\n```\r\n\r\nNow, as I expected,\r\n```julia\r\nbar.foo ∈ keys(bar.fooDict) # returns true\r\nbar.foo ∈ collect(keys(bar.fooDict)) # returns true\r\nbar2.foo == first(keys(bar2.fooDict)) # returns true\r\nbar2.foo === first(keys(bar2.fooDict)) # returns true\r\n```\r\nHowever,\r\n```julia\r\nbar2.foo ∈ keys(bar2.fooDict) # returns false!\r\nbar2.foo ∈ collect(keys(bar2.fooDict)) # returns true!\r\nbar2.fooDict[bar2.foo] # KeyError!\r\n```\r\non both 0.5.0 (3c9d753) and commit d7b6ac3 (8 days old master).\r\n\r\nExamining further, [`in` for `KeyIterator{Dict}`](https://github.com/JuliaLang/julia/blob/v0.5.0/base/dict.jl#L702) uses [`ht_keyindex`](https://github.com/JuliaLang/julia/blob/v0.5.0/base/dict.jl#L532). In `bar2.fooDict.keys`, the `foo` object appears at position 14, as it does in `bar.fooDict.keys`. However, `Base.hashindex(bar2.foo, length(bar2.fooDict.keys))` is 2, while `Base.hashindex(bar.foo, length(bar.fooDict.keys))` is 14 as expected. This in turn is a result of the fact that `hash(bar.foo)` is not the same as `hash(bar2.foo)`, since the hashes are based on `object_id`s, which are of course different.\r\n\r\nSo I guess a more specialized version of `Base.deepcopy_internal` is required for `Dict`?\r\n"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/271176708","html_url":"https://github.com/JuliaLang/julia/issues/19921#issuecomment-271176708","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/19921","id":271176708,"user":{"login":"tkoolen","id":2585880,"avatar_url":"https://avatars.githubusercontent.com/u/2585880?v=3","gravatar_id":"","url":"https://api.github.com/users/tkoolen","html_url":"https://github.com/tkoolen","followers_url":"https://api.github.com/users/tkoolen/followers","following_url":"https://api.github.com/users/tkoolen/following{/other_user}","gists_url":"https://api.github.com/users/tkoolen/gists{/gist_id}","starred_url":"https://api.github.com/users/tkoolen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tkoolen/subscriptions","organizations_url":"https://api.github.com/users/tkoolen/orgs","repos_url":"https://api.github.com/users/tkoolen/repos","events_url":"https://api.github.com/users/tkoolen/events{/privacy}","received_events_url":"https://api.github.com/users/tkoolen/received_events","type":"User","site_admin":false},"created_at":"2017-01-08T20:24:17Z","updated_at":"2017-01-08T20:24:17Z","body":"Here's what I came up with:\r\n```julia\r\nfunction deepcopy_internal(x::Dict, stackdict::ObjectIdDict)\r\n    if haskey(stackdict, x)\r\n        return stackdict[x]::typeof(x)\r\n    end\r\n\r\n    if isbits(eltype(x))\r\n        return (stackdict[x] = copy(x))\r\n    end\r\n\r\n    dest = similar(x)\r\n    stackdict[x] = dest\r\n    for (k, v) in x\r\n        dest[Base.deepcopy_internal(k, stackdict)] = Base.deepcopy_internal(v, stackdict)\r\n    end\r\n    dest\r\nend\r\n```\r\n\r\nI'm having some trouble compiling Julia on OSX at the moment; I can test and submit a PR when I get back to a Linux machine."}}
{"actor":{"display_login":"tkoolen"}}