Last active
April 28, 2022 09:47
-
-
Save Fusselwurm/64b0e54778e1c364f85e1b6bbc6e4fd6 to your computer and use it in GitHub Desktop.
`private` is important to use in nested for loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// not using private for `_i` | |
_x = ""; | |
for [{_i = 1}, {_i <=2}, {_i = _i + 1}] do { | |
_x = _x + "o"; | |
for [{_i = 1}, {_i <= 2}, {_i = _i + 1}] do { | |
_x = _x + "i"; | |
}; | |
}; | |
_x == "oii" // welp. inner loop re-used the outer variable | |
// do use private to prevent re-using | |
_x = ""; | |
for [{_i = 1}, {_i <=2}, {_i = _i + 1}] do { | |
_x = _x + "o"; | |
for [{private _i = 1}, {_i <= 2}, {_i = _i + 1}] do { | |
_x = _x + "i"; | |
}; | |
}; | |
_x == "oiioii" | |
// yay. | |
// HINT functions do inherit the scope from the caller. | |
// a `for` loop in, say, a CBA event loop may create an _i that you inadvertently re-use. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment