Declaring variables before a for loop necessary in some cases?
-
Hi friends, it's been a minute.
So my question is about declaring loop variables before a for loop, given that HISE doesnt like it inside the loop.
There seems to be conflicting guidance (and examples in the wild) about whether loop variables should be declared beforehand, and how much they can interfere with other functions.
Both versions seem to work most of the time, but in larger namespaces with many inline functions, I’ve seen odd behavior that looks like iterator variables leaking or being reused across functions (off-by-one bugs, wrong source index, etc.).
My questions
Are loop variables (i, s, n, etc.) implicitly local to the inline function, or are they shared unless explicitly declared local?
Is it considered best practice to always declare loop iterators as local at the top of each inline function?
Can undeclared loop variables interfere with other inline functions in the same namespace, or is that a myth?
I'm looking for some clarity as I’m trying to decide whether it’s worth doing a cleanup pass where every inline function explicitly declares its loop iterators (e.g. local i, s, n;) to avoid subtle bugs, or whether that’s unnecessary.
Thanks.
-
@CyberGen said in Declaring variables before a for loop necessary in some cases?:
I’ve seen odd behavior that looks like iterator variables leaking or being reused across functions
Are you using an old version of HISE? I remember having a similar issue in some specific circumstances a few years ago. Where you have a loop in an inline function calling another inline function which also has a loop. In that situation I worked around it by declaring the iterator variable in each function. But other than that situation I wouldn't declare iterators.
Here's a test, I'm not seeing an issue in the latest version of HISE.
HiseSnippet 769.3ocsUstaSCCE1taAQCWDShG.q8qN51TS1kNz.wX8BTAcqhNl3eStNNqVK0NJwYPEZuT7jwa.bbRVSKqZWpDQUM4b+KG+cNoWjhwiiUQHr8IiC4H7Ss5OVpG1XHUHQcZhvO2pKMVyiHYpNbbHMNl6gv3k9fQAt7xnzqe+tCoATIiWnBgNUIX7OKFIzEZ6cvmDAAsod7SDilx6sOnCSIanBTI.dVxpFJjxtfdN+HpwsRVnORiGhvuxh6uy.mZd9be25udP85d07bYaQc10k55y2iMn91TWvID9Qs7DZUTeMUyigjdnxab+gpuKyJvohXwf.tQvA0GpblZTigh.udW2bhQH7x8JZUKk0pdoUWgmXh9hV1KRMPJhX5lFtzsAImG.jvSAokyfzJV8YQhPcgECddhUGIbB5SgylogRlunR+Ba0PAdH0aNhdAucDHLIhJ6Vq15D3u0121VBmDwvoBmbBOVa+S6xBYfPxI9IRlVnjD3cR6TYM6xfsx9pHREA4sjZ6SDj2PbL2qVErlZtLT0XU.eyvHgTWY0M1XCxpjpfqUg6fzpPMA2L4zsR5yWYa9M+x5tXkUTj3qrssMuXal+Vr+j7qjGoz7ikPIfT.NR9WS99y0lowFoBB3Qy0rYBH51BrhLYz.dz5jKoAI7INBm3yRidz8iFwxNmmxQkriTnONjmK2VE3YnGlmuIoCkSTfm9ZmlTM0vCy0A9ExizBCbvM4WBS9YrxxVM4wWnUgvr+MnrvvhxKIfpmcBxraI2.zOlg1ZnlxXgd7z6dd.iU0t0wp6KDWwpmPyFNeLVZNXD5T+OvX9xnmY0x2myzE.bYq1eaQ27bGk+KpDsPddWpNR7CD15njQ8gk1LNTcojG.I2BWxvVxjqYjMcf9boWpvefqbiNFYbtQmqMhFQYQpyXYSAl0cONUCfIY5mCJCeWBjINnzIio6yifsumwXylpaDn6hF3VKZfaunAtyhF3tKZf0Wz.26tCz7ww2mnUixFaPnt8ZktJAiaIo.CLksh9KUwoVr. -
@CyberGen I always try to use
for (p in panels)instead of regularfor ++loops wherever I can for this reason. Feels like thepis more clearly defined there.The Loops section on the Coding Standards doc page probably has everything you need: https://docs.hise.dev/scripting/scripting-in-hise/hise-script-coding-standards.html
This is because by default iterators exists in the global scope, declaring the function's iterator as local prevents it conflicting with the global version.
-
@dannytaurus said in Declaring variables before a for loop necessary in some cases?:
The Loops section on the Coding Standards doc page probably has everything you need
I think it's out of date, I'll need to revise it.
This is where that stemmed from:
https://forum.hise.audio/topic/2157/inline-functions-sharing-iterators/9
Or
https://forum.hise.audio/topic/132/iterator-variable-clashes?_=1770636397075But the example in that thread no longer has an issue with the latest version of HISE.
-
Ah yeah, here's where it was fixed: https://forum.hise.audio/topic/5370/possible-breaking-change-local-variables-references-inside-nested-function-definitions/60?_=1770636622217
-
@David-Healey So much for RTFM!

-
-
@David-Healey & @dannytaurus thank you guys. I'll update.