[CLARIFICATION] Performing a merge on a local variable also modifies the global variable

:warning: This topic started as a bug report but was later converted into a resource, thanks to Joe90!

I checked out Kodular Fenix (v1.5D.0) Bug Tracker before creating a new topic

Premise

This report comes from the fact that, while working on a project, I was going crazy trying to understand what was happening.
I want as many people as possible to be aware of it, so they don’t waste time figuring out where the issue is, even though it’s easily fixable.

Describe your issue

When merging two dictionaries into a local variable, the global variable is also affected.

Steps to reproduce the issue

  • Create a local variable (tempDictionary) and initialize it with the value of a dictionary variable (var1).
  • Merge the local variable with another dictionary (var2).

Expected Behaviour

The global variable should not be affected.

Actual Behaviour

The global variable gets modified as if the merge was performed directly on it.

Show your Blocks


MergeTest.aia (3.2 KB)

Android version

14

Fix by me

Fix by Joe90 :heart_hands:

1 Like

Not really a bug, just an inconvenience!

Easily solved, use the copy dictionary block when you set the global dictionary variable to the local variable

image

(This is the same with lists, where you also use a copy list block)

2 Likes

Hi Joe,

Thanks a lot, that’s also a great solution.

Honestly, I was unsure whether to report it or not, but it seemed like too unusual a behavior.
My concern was that someone else might run into the same issue as I did and end up wasting time unnecessarily.

More importantly, I would have really liked to understand the reason behind this behavior.

1 Like

Your method, without the copy block, “links” the dictionaries/variables, whilst using the copy block “unlinks” them, creating, in effect, a new dictionary in the local variable. This, I understand, is by design from AppInventor.

2 Likes

Given my lack of experience, I did some studying and discovered that different programming languages behave this way depending on whether they use reference semantics or not.
So, as you correctly pointed out at the beginning, I now fully agree that it is not a bug, but simply how the system works.

Thanks again, Joe, you always teach me something new :heart_hands:

Moved to Guides > Resources