I just pushed a commit where the compiler hoists variables with default initializers.
- Josh On 2019/03/19 10:34:05, Harbs <[email protected]> wrote: > The latest compiler changes exposed a problem: > > for(var i:int=0;i<len;i++){ > var foo:Object; > if(someCondition(thingy[i]){ > foo = calculateFoo(thingy[i]); > } > doSomethingWithFoo(foo); > } > > Compiles to: > for(var i=0;i<len;i++){ > var foo = null; > if(someCondition(thingy[i]){ > foo = calculateFoo(thingy[i]); > } > doSomethingWithFoo(foo); > } > > This is a change from the intended behavior and foo is nullified in each step > of the loop. This can cause doSomethingWithFoo(null) even when foo was a > valid value in a previous iteration. > > I’d suggest two things: > > 1. I don’t see the value of initializing Objects to null at all. I’m not sure > why that became the default. I’d vote for not defaulting to initializing > Objects (just other types). > 2. Local values of any type should not be initialized in a loop. > > Harbs
