Coral icon indicating copy to clipboard operation
Coral copied to clipboard

Variables defined in for, while, or range loops which do not run are defined in the outside scope

Open jacobaustin123 opened this issue 6 years ago • 0 comments

If a variable is defined in a for, while, or range loop which does not run because (a) the list is empty (b) the condition is initial false, or (c) the integer is less than or equal to zero, it will be transformed upon leaving the (un-run) loop and consequently defined. This is because STransforms are added as SStage(entry, loop, exit), and the entry and exit blocks, which contain the STransforms, run regardless of the condition. This can be fixed by refactoring the While, Range, and For types to include the entry and exit conditions, and only running them if the condition is true/valid. A basic example is:

for i in []:
    x = 4
    print(x)
print(x)

i = 0
while i > 0:
    x = 4
    print(x)

print(x)

jacobaustin123 avatar Jan 12 '19 05:01 jacobaustin123