In functions (and the global scope), u32 var = 123;
is equivalent to u32 var; var = 123;
. In pattern structs, u32 var = 123;
creates a local variable (not part of the pattern data) that can be freely modified, whereas u32 var; var = 123;
is simply forbidden (as u32 var;
is parsed as part of the pattern data, and therefore must not be modified) - there is a functional difference between initialised and uninitialised variables/patterns.
However, arrays can't be initialised like that; neither u32 var[3] = {10,20,30};
, u32 var[3] = [10,20,30];
, u32 var[3] = 10,20,30;
, nor u32 var[3] = 10;
are allowed. This means that it's impossible to declare local array variables (not part of the pattern data) within pattern structs. I would've suggested "let us initialise arrays", but the same applies to structs (e.g. struct MyStruct { u32 value; }; MyStruct var = {0};
doesn't work), and I can't think of a good way to initialise structs while declaring them.
Currently, to get around that, you have to first make a section, then place an array or struct in that section, and then fill out the array/struct values. Doing this can be inconvenient, fills the Sections tab with many different sections if you e.g. create a section like this in a struct and use that struct 20 times, and makes the [[export]]
attribute not display it in the Pattern Data tab (as you instead have to open and look within the respective section's own patterns).
Therefore, I suggest an attribute or a keyword to make a local variable even when it's not initialised. For example, u32 var[3] [[local]];
would create a local array with 3 zeroes, and struct MyStruct { u32 value; }; MyStruct var [[local]];
would create a local struct with value
set to zero.
(For structs, care should be taken to ignore @
for its members, except when it's placed in a section created within that struct or a sub-struct (or just except when it's in any section at all?). Structs that do something like struct MyStruct { u32 value1; if (value1 > 10) u32 value2; }
won't work as expected as value1
would always be zero at that time, but it's up to pattern makers to not use local structs that require being placed in memory.)
TL;DR: An attribute/keyword to force a variable to be local would be nice, even if it's not initialised (which is currently impossible for arrays/structs). All values that would've been read from (non-section) memory should probably default to 0.