language agnostic - Does a for loop create two scopes? -
imagine following statement (from imaginary c-like language) gets desugared more simple form:
1| (int = 0; < 10; ++i) 2| { 3| // work 4| }
of course, sticking point int = 0
in initializer, beacuse suppose:
- after loop done,
i
out of scope , can't reference it. - upon reaching line 2, new scope pushed on stack , popped on line 4.
this mean specific for-loop desugar to:
{ int = 0; while (i < 10) { // work ++i; } }
leading creation of scope sole purpose of containing incrementer variable.
i understand specifics implementation defined language permits declarations inside classic-style loops. i'm curious if how work under covers, @ least when creating initial ast.
i think thats compiler your. can see if try compile both codes on llvm e.g. clang -s -emit-llvm , compare results.
wiki
Comments
Post a Comment