Hi @raphbaph,
There isnât actually a problem with @andrewflemingâs code, as you can see from the reply he gotâŚ
If you deploy his code yourself, you will see that it works perfectly.
You are right that the parameter newWord is stored temporarily in memory, but it is then pushed directly to the array which is saved in persistent storage. This is effectively carrying out the same type of assignment as your solution to the Data Location AssignmentâŚ
⌠where the temporarily-stored balance
is directly assigned to the users
mapping in persistent storage.
State variables (which include arrays), mappings and structs are saved in persistent storage by default, and therefore do not require us to explicitly define their data location. However, as with strings and struct instances, we do need to do this with arrays when they are assigned to a local variable.
If you mean a variable within the addToPoem function, then this wouldnât be a state variable. All variables defined within functions are local, and are therefore lost when the function finishes executing. Do you mean we could define a local variable with the keyword storage
in order to create a pointer to a value saved in persistent storge? As far as Iâm aware, this is only a useful alternative to direct assignment with struct instances, like we did with the alternative solution in the Data Location AssignmentâŚ
User storage user = users[id]; /* This is a pointer to a specific
struct instance saved in a mapping */
user.balance = balance;
Have you tried implementing your suggestion to see if it works? If so, it would be interesting to see the code to get a better idea of your thinking.
Itâs great that you are thinking about how to apply what youâve learnt about data location to other scenarios. This post is meant to help you with that, and certainly not to discourage you from participating in these discussion topics. Some of these data location concepts are complex, and take a while to fully understand and master, but getting actively involved in forum discussions with certainly help with that.
Let me know if anything Iâve explained is unclear, or if you have any questions 