Copy dictionary doesn't work OR what am I missing?

Hi,

I'm trying to use the "copy dictionary" block in order to initialize specific different dictionaries to a master/template dictionary.

When I change the value of a key in one dictionary, the other one is affected as well.
This implies that the copy didn't work, and that the assignment is "by reference" and not "by value"...

OR: Am I missing something?

See mini-example: (The notifier prints both values as "true")

Thanks in advance!

Use a clock component, set time interval for example to 200ms and then when clock timer change values in dictionaries

2 Likes

Thanks for your quick reply.

I've tried your suggestion, but it still changes both dictionaries.

Check_Copy_Dict.aia (3.9 KB)

You are right setting values for key path somehow affects all dictionaries

Changing value like this works

Check_Copy_Dict_1.aia (3.7 KB)

1 Like

Thanks again for your efforts.
Well, your workaround makes the dictionary usage hard or even impossible in my app.
I have a complex dictionaries structure, with procedures that interact with the user, that result in some changes in the structure's values and also check some values during work.

If there is a bug, who should we inform and how?

Interestingly the documentation says this:

image

The copy dictionary makes a deep copy of the given dictionary. This means that all of the values are copied recursively and that changing a value in the copy will not change it in the original.

But the tooltip says this:

Returns a shallow copy of the dictionary

Will assign to Evan at MIT

3 Likes

While this is being fixed, maybe write a general deepCopy value procedure that will return a deep copy of its input?

That makes your code simpler, if you replace your copy calls with deepCopy calls.

I can take a whack at it.
(There will be recursion).

P.S. Here it is ...

Capture Deep_Copy_Dict.aia (4.0 KB)

The recursive procedure could possibly be made tighter using the value if/then/else block, but this is good enough for jazz, as my musician wife says.

P.P.S. Added to my list of project samples at

1 Like

Thanks a lot!

Perhaps you can instead use the list blocks? There the "copy" works correctly. To retrieve an item use then "look up for pairs". If needed there are a few converting blocks in the "dictionary" collection between lists and dictionary.

Here is a version of deepCopy that avoids any potential problems with the list copy block's handling of dictionaries ...

(edit - ABG)

P.S. These blocks can be dragged directly into your Blocks Editor workspace.

See A way to group projects by theme
for a demo.

4 Likes

I'll need to think about this. It's correct that as implemented the code only does a shallow copy (via HashMap's clone method). I'd have to go look at the feature branch development and see when we decided to change it from deep copy to shallow copy. I think at least part of the problem was to avoid the issues of having to clone cyclical structures, but I don't recall.