As asked in this question for C, https://www.linuxquestions.org/questions/programming-9/assigning-variables-to-multiple-fields-in-a-structure-is-tiresome-897337/, is it possible to assign variables to multiple fields in a structure, without referencing the structure for each variable? Something like

struct Foo {
   uint   x;
   uint   y;
   uint   z;

Foo foo;

foo = { .x = 2, .z = 12 }

Edit: the post I referenced seems to be about initializing structs, I'm asking more about updating multiple variables at once. When this is convenient, here is an example:

mapping(uint => Data) data;

function schedule() returns (uint);

and then

data[schedule()].x = ;
data[schedule()].z = ;
data[schedule()].otherRandomThing = ;
data[schedule()].foo = ;
data[schedule()].lala = ;


data[schedule()] = 
x: ,
z: ,
otherRandomThing: ,
foo: ,

I'm all for that its a dumb question, don't really care, my code is more readable if I don't have to write data[schedule()] in front of every variable I update, and that's what I care about here. The question on calling functions in contexts I asked earlier is obviously related to this, as it is another approach to increase readability.


No, solidity doesn't support that syntax.

You can create a temporary reference and modify that instead.

Data storage myData = data[schedule()];

myData.x = 1254;
myData.z = 0x576657220676f6e6e61206769766520;
myData.otherRandomThing = "u";
myData.foo = 8309;
myData.lala = "p";
  • that might be a good approach. Googled and found your answer on that here too, ethereum.stackexchange.com/a/61007. to someone who does not get the whole picture, are there any drawbacks? is it recommended? would you use that if there are 10 or so variables in the struct that are altered? since I'm a bit new still, a bit like flying blind, just know that all the data[schedule] makes it look cluttered af – Johan Sep 30 '20 at 22:07
  • @Johan It is pretty common to use storage reference (Data storage myData;) if you want to modify a few fields, and use a memory copy (Data memory myData;) if you modify many of them (then update storage once after all modifications). When learning it is best to try following the best developers, I think the OpenZepplin's contracts are well maintained and their developers are pretty good. – Ismael Oct 1 '20 at 0:05
  • have followed the best developers for years. the point is, seems like going a few steps further than necessary to add "temporary reference" when its all about code readability. so I asked if you recommended doing so. is the temporary reference compiled down to the equivalent of what I was thinking of, or, is data being stored in the contract? etc, similar things. do you know the url to the solidity documentation on it? – Johan Oct 1 '20 at 9:40
  • the words used seem a bit arbitrary. storage variables are not allowed to be declared in functions. so, "storage" is then re-used to just mean "alias"? seems a bit arbitrary, but it's likely I miss a few thousand things around that – Johan Oct 1 '20 at 10:02
  • doesn’t really matter anyway since recursive public structs (with getter methods) aren't allowed anyway so I'm probably going to have to go with a clumpsier method. – Johan Oct 1 '20 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.