allow assigning values to nested dictionaries #440
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an issue where modifying a nested dictionary did not work:
Would generate an achievement worth 3 points, not 5.
This is a result of the temporary variable used for storing
dict["a"]
evaluating the dictionary instead of referencing it. By evaluating it, a copy was made, and the["b"] = 5
modification was applied to the copy.The same problem occurs when using a non-anonymous intermediate variable:
t
was a copy ofdict["a"]
, so the modification was applied to the copy.When dictionaries (or arrays) are passed to a function, they're done so by reference, so the function can modify the dictionary. I've extended that logic to do assignments of dictionaries (and arrays) by reference. As such, both
t
and the temporary variable in the first example are references to the nested dictionary, allowing the modification to go through.NOTE: This eliminates the ability to copy a dictionary (or array) just through assignment:
needs to be changed to:
This also provides a performance improvement when working with nested dictionaries as copies of the subdictionaries are no longer made just to pull values from them.