global_map changes if local_map changes

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
TheBrain1984
Posts: 137
Joined: 07 Aug 2013 08:17
Location: Germany

global_map changes if local_map changes

Post by TheBrain1984 » 02 Dec 2017 10:50

Hello (Martin),

for almost every var the allocation in a script is a call by value allocation. So if you have a expression like this:

Code: Select all

local_var = global_var;
anyChangingFunction(local_var)
the local_var will get the effect of the Function and the global_var not. If global_var is a map the behaviour is diffrent. the global_var will get any effect that local_var will get. So if you add a MapEntry to local_var, then global_var will have this Element too.

Is this a bug or a feature? If this is a feature is there a fast and effective way to create a local_var that isn't linked with the global_var.

If it is a bug and you're going to fix it, can you please implement a way where this link wont't be lost, because I already implemented a few scripts where I use this "bug".

Thanks

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: global_map changes if local_map changes

Post by Desmanto » 02 Dec 2017 14:57

This is featured or behave as it should, won't be "fixed", since it is not a bug. I've ever faced the same problem, but with list. I remember there are functions called copyList() and copyMap(), which I have read at the beginning when switching to Automagic. At that time, I still don't understand what is the meaning of "flat copy".

Can't remember where I read the explanation, maybe in python, java or automagic documentation itself. List and Map are objects. When assigning them to a new variable, it only assign the reference (pointer) to the address of the object. The object itself doesn't change. So in your case, both local and global are pointing to the same address of the Map. When you change, add/remove elements or value; both will changed. Since both pointing to the same address. So it is not about local and global variable, it happens in all variable.

To copy the whole object completely, not only the reference (pointer), you should use copyMap() for Map or copyList() for list. This not only works for local vs GloVar and vice versa. But works for local to local or GloVar to GloVar too. It simply create a flat copy of the same Map, and the new variable will point to new address of the new Map. Changing the new Map won't affect the copied old Map.

==================
Using this idea, we can change the gloVar without refering to it. Just assign a local variable to it, and only make changes to the local; which will be reflected to GloVar. But I don't think it is a good practice. Since it probably can cause a logic flaw in the future; which is more disastrous than a big fatal error.

Example, you have a GloVar storing a Map of phone number and messages. You are trying your flow, and found out something won't work. So you test it out in separate script just by usual assignment (not using copyMap()). During your testing, maybe you remove some keys and values, replacing them with dummy phone number or maybe dummy messages. After you have finish, you cleanup and try to send text message again. Now, the text message will still be sent, but I don't it will be sent to who anymore. It won't give fatal error, since there is no error. But it is a logic flaw, since the messages now sent to the wrong number or worst; correct number with wrong messages. It will be a big problem, if somehow, your dummy messages were something like, "I love you", "I hate you", or any other dangerous sentences; sent to your friends or family. :shock:

OK, I am over exaggerated it. But it can happens. So it is better to avoid from the very first.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
TheBrain1984
Posts: 137
Joined: 07 Aug 2013 08:17
Location: Germany

Re: global_map changes if local_map changes

Post by TheBrain1984 » 04 Dec 2017 12:12

thx

Post Reply