Is there a way to limit the scope of variables declared in a loop?
-
Here, the variable in question is "columnID":
// --- inside an inline function --- // init the filterData storage in the container as an object dataTableContainer.data.filterData = {}; for (i = 0; i < numVisibleCols; i++) { local columnID = columnNames[i]; dataTableContainer.data.filterData[columnID] = { "searchValue" : undefined, "selectedFilterValues": [], "anyAll" : undefined, "minValue" : undefined, "maxValue" : undefined }; }
Any use of "columnID" later on outside the loop will try to use that declared version, so throwing errors in callbacks and paint routines.
I can, of course just rename the variable to "columnID1" or some other variant,
but wondering if there is a more elegant solution to limiting the scope of the variable to just the loop, so I can reuse "columnID". -
Wrap whatever you're doing in an inline function.
inline function test() { for (i = 0; i < 10; i++) { local count; count = i; } } test(); Console.print(count);
-
@d-healey It's already inside an inline function though.
-
@VirtualVirgin And that's the only variable with the name
columnID
that you've declared in your script? -
@VirtualVirgin said in Is there a way to limit the scope of variables declared in a loop?:
solution to limiting the scope of the variable to just the loop
'local' means that its only local to that scope and should be able to use it again within another scope separately. If its being called outside of this inline function, you might have it declared somewhere else.
When something like this is happening to me I use this and 'find all occurrences' of whatever you searching for. Its good just to be sure. (right click in the code editor to show this list)
-
I think my question here is being misinterpreted or misunderstood.
The question again is "Is there a way to limit the scope of variables declared in a loop?".
In Javascript, there are options for declaring a variable that is limited to the scope of the loop,
such as "let".I understand HISE is not Javascript, but I was just asking if there was something similar in HISE in order to do this
because "local" and "var" are not limited to the scope of a loop: -
@VirtualVirgin said in Is there a way to limit the scope of variables declared in a loop?:
Is there a way to limit the scope of variables declared in a loop?"
Nope.
Local variable scope is limited to the inline function.