Re: variable declaration reset

new topic     » goto parent     » topic index » view thread      » older message » newer message
euphoric said...

That's the behavior I expect, no matter what language I'm using. A variable defined is a variable reset.

You could argue it that way, however the point of block scope is to only declare a variable where it is needed, and clearly prev is only needed within the loop and we don't want any code outside the loop to be able to see it. However if you reset it on every iteration then it is absolutely impossible to declare a local prev within the loop for the use I had in mind. The closest you could get would be

if true then 
    integer prev 
    for 
        ... 
    end for 
end if 

which is potentially an awful lot of manual re-indentation just to introduce a new variable and limit it to the desired scope.

In fact, now I think of it, I can almost imagine a more complicated bit of code where you want two prev, and genuinely want them as separate entities, and want their values preserved even while code execution leaves and re-enters their scope many times. Maybe if you had two such lists and wanted to advance the one with the lowest curr first?

Mind you, there is a valid counter argument in that variables should really be reset to unassigned at the end of/when execution leaves their scope, not that Phix currently does that.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu