Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
It's valid both in a frame script in an IDE and in an AS3 class compiled via mxmlc. I suspect it's also valid in an block, but I didn't bother testing. Actually give it a try before telling me it doesn't work. :) On Thu, Mar 27, 2008 at 6:59 PM, Steven Sacks <[EMAIL PROTECTED]> wrote: > Valid where? If that's in a class function and i is not a class > variable, then the compiler will complain that you're using an > undeclared variable. > > > Cory Petosky wrote: > > I guess I should have provided an example when I mentioned no block > > level scoping. Try this on for size: > > > > for (i = 0; i < 10; ++i); // Do nothing but increment i > > var i:int; > > trace(i); > > > > This is totally valid code and will trace 10! ALL variable > > declarations in a function, regardless of the block the variable is > > declared in, are performed as the function is pushed on the stack. > > > > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- Cory Petosky : Lead Developer : PUNY 1618 Central Ave NE Suite 130 Minneapolis, MN 55413 Office: 612.216.3924 Mobile: 240.422.9652 Fax: 612.605.9216 http://www.punyentertainment.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
Valid where? If that's in a class function and i is not a class variable, then the compiler will complain that you're using an undeclared variable. Cory Petosky wrote: I guess I should have provided an example when I mentioned no block level scoping. Try this on for size: for (i = 0; i < 10; ++i); // Do nothing but increment i var i:int; trace(i); This is totally valid code and will trace 10! ALL variable declarations in a function, regardless of the block the variable is declared in, are performed as the function is pushed on the stack. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
I guess I should have provided an example when I mentioned no block level scoping. Try this on for size: for (i = 0; i < 10; ++i); // Do nothing but increment i var i:int; trace(i); This is totally valid code and will trace 10! ALL variable declarations in a function, regardless of the block the variable is declared in, are performed as the function is pushed on the stack. Cory On Thu, Mar 27, 2008 at 3:47 PM, jonathan howe <[EMAIL PROTECTED]> wrote: > "I wish it did." > > Maybe at least one person saying that was what I was looking for... Thanks > for the discussion! > > > > On Thu, Mar 27, 2008 at 4:22 PM, Ian Thomas <[EMAIL PROTECTED]> wrote: > > > AFAIK, in AS2 the Flash IDE didn't respect block level scoping, but > > MTASC did, which led to some confusion. That leads some people to > > think that AS2 as a language has block level scoping. > > > > AS3 definitely doesn't respect block scopes, and I curse every time I > > trip over that 'variable declared twice' issue. I wish it did. > > > > Ian > > > > On Thu, Mar 27, 2008 at 8:09 PM, Juan Pablo Califano > > <[EMAIL PROTECTED]> wrote: > > > for (var i:int = 0; i < 10; i++) > > > > > > { > > > if (i == 5) break; > > > } > > > trace(i); > > > > > > Mmm, have you actually tested the example? Because it does trace 5, > > since, > > > as it was explained earlier in this thread, there is no block level > > scoping > > > in AS 3.0. In fact, and this was mentioned too, all var declarations > > are > > > "moved up" to be executed as the first actions run in a function's code > > (I > > > believe that was called hoisting, but I might be wrong). > > > > > > Cheers > > > Juan Pablo Califano > > > > > > 2008/3/27, Steven Sacks <[EMAIL PROTECTED]>: > > > > > > > > > > > > > > function doSomething > > > > > { > > > > > var i:int; > > > > > for(i=0;i++;i<10) > > > > > { > > > > > } > > > > > } > > > > > > > > > > Is functionally identical to this: > > > > > > > > > > function doSomething > > > > > { > > > > > for(var i:int =0;i++;i<10) > > > > > { > > > > > } > > > > > } > > > > > > > > Wrong. It's not. > > > > > > > > In the latter example, i is not available after the loop. In the > > first > > > > example, it is. > > > > > > > > var i:int; > > > > for (i = 0; i < 10; i++) > > > > { > > > > if (i == 5) break; > > > > } > > > > trace(i); > > > > -- 5 > > > > > > > > There are a multitude of uses for this, and I do it all the > > > > time. Additionally, I read somewhere many moons ago (back in my > > FLASM days) > > > > that declaring variables outside a for loop is less bytecode and uses > > less > > > > memory. I don't believe that applies to the counter declaration, but > > I do > > > > know it applies to the comparison as well as vars declared inside the > > for > > > > loop. However, this level of optimization is only useful in a > > practical way > > > > on mobile and some games. > > > > > > > > ___ > > > > Flashcoders mailing list > > > > Flashcoders@chattyfig.figleaf.com > > > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > > > ___ > > > Flashcoders mailing list > > > Flashcoders@chattyfig.figleaf.com > > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > > > -- > -jonathan howe :: 404.434.2321 :: 180 High St Apt 26 Portland, ME 04101 > ___ > > > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- Cory Petosky : Lead Developer : PUNY 1618 Central Ave NE Suite 130 Minneapolis, MN 55413 Office: 612.216.3924 Mobile: 240.422.9652 Fax: 612.605.9216 http://www.punyentertainment.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
"I wish it did." Maybe at least one person saying that was what I was looking for... Thanks for the discussion! On Thu, Mar 27, 2008 at 4:22 PM, Ian Thomas <[EMAIL PROTECTED]> wrote: > AFAIK, in AS2 the Flash IDE didn't respect block level scoping, but > MTASC did, which led to some confusion. That leads some people to > think that AS2 as a language has block level scoping. > > AS3 definitely doesn't respect block scopes, and I curse every time I > trip over that 'variable declared twice' issue. I wish it did. > > Ian > > On Thu, Mar 27, 2008 at 8:09 PM, Juan Pablo Califano > <[EMAIL PROTECTED]> wrote: > > for (var i:int = 0; i < 10; i++) > > > > { > > if (i == 5) break; > > } > > trace(i); > > > > Mmm, have you actually tested the example? Because it does trace 5, > since, > > as it was explained earlier in this thread, there is no block level > scoping > > in AS 3.0. In fact, and this was mentioned too, all var declarations > are > > "moved up" to be executed as the first actions run in a function's code > (I > > believe that was called hoisting, but I might be wrong). > > > > Cheers > > Juan Pablo Califano > > > > 2008/3/27, Steven Sacks <[EMAIL PROTECTED]>: > > > > > > > > > > function doSomething > > > > { > > > > var i:int; > > > > for(i=0;i++;i<10) > > > > { > > > > } > > > > } > > > > > > > > Is functionally identical to this: > > > > > > > > function doSomething > > > > { > > > > for(var i:int =0;i++;i<10) > > > > { > > > > } > > > > } > > > > > > Wrong. It's not. > > > > > > In the latter example, i is not available after the loop. In the > first > > > example, it is. > > > > > > var i:int; > > > for (i = 0; i < 10; i++) > > > { > > > if (i == 5) break; > > > } > > > trace(i); > > > -- 5 > > > > > > There are a multitude of uses for this, and I do it all the > > > time. Additionally, I read somewhere many moons ago (back in my > FLASM days) > > > that declaring variables outside a for loop is less bytecode and uses > less > > > memory. I don't believe that applies to the counter declaration, but > I do > > > know it applies to the comparison as well as vars declared inside the > for > > > loop. However, this level of optimization is only useful in a > practical way > > > on mobile and some games. > > > > > > ___ > > > Flashcoders mailing list > > > Flashcoders@chattyfig.figleaf.com > > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- -jonathan howe :: 404.434.2321 :: 180 High St Apt 26 Portland, ME 04101 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
AFAIK, in AS2 the Flash IDE didn't respect block level scoping, but MTASC did, which led to some confusion. That leads some people to think that AS2 as a language has block level scoping. AS3 definitely doesn't respect block scopes, and I curse every time I trip over that 'variable declared twice' issue. I wish it did. Ian On Thu, Mar 27, 2008 at 8:09 PM, Juan Pablo Califano <[EMAIL PROTECTED]> wrote: > for (var i:int = 0; i < 10; i++) > > { > if (i == 5) break; > } > trace(i); > > Mmm, have you actually tested the example? Because it does trace 5, since, > as it was explained earlier in this thread, there is no block level scoping > in AS 3.0. In fact, and this was mentioned too, all var declarations are > "moved up" to be executed as the first actions run in a function's code (I > believe that was called hoisting, but I might be wrong). > > Cheers > Juan Pablo Califano > > 2008/3/27, Steven Sacks <[EMAIL PROTECTED]>: > > > > > > function doSomething > > > { > > > var i:int; > > > for(i=0;i++;i<10) > > > { > > > } > > > } > > > > > > Is functionally identical to this: > > > > > > function doSomething > > > { > > > for(var i:int =0;i++;i<10) > > > { > > > } > > > } > > > > Wrong. It's not. > > > > In the latter example, i is not available after the loop. In the first > > example, it is. > > > > var i:int; > > for (i = 0; i < 10; i++) > > { > > if (i == 5) break; > > } > > trace(i); > > -- 5 > > > > There are a multitude of uses for this, and I do it all the > > time. Additionally, I read somewhere many moons ago (back in my FLASM > days) > > that declaring variables outside a for loop is less bytecode and uses less > > memory. I don't believe that applies to the counter declaration, but I do > > know it applies to the comparison as well as vars declared inside the for > > loop. However, this level of optimization is only useful in a practical > way > > on mobile and some games. > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
for (var i:int = 0; i < 10; i++) { if (i == 5) break; } trace(i); Mmm, have you actually tested the example? Because it does trace 5, since, as it was explained earlier in this thread, there is no block level scoping in AS 3.0. In fact, and this was mentioned too, all var declarations are "moved up" to be executed as the first actions run in a function's code (I believe that was called hoisting, but I might be wrong). Cheers Juan Pablo Califano 2008/3/27, Steven Sacks <[EMAIL PROTECTED]>: > > function doSomething > > { > > var i:int; > > for(i=0;i++;i<10) > > { > > } > > } > > > > Is functionally identical to this: > > > > function doSomething > > { > > for(var i:int =0;i++;i<10) > > { > > } > > } > > Wrong. It's not. > > In the latter example, i is not available after the loop. In the first > example, it is. > > var i:int; > for (i = 0; i < 10; i++) > { > if (i == 5) break; > } > trace(i); > -- 5 > > There are a multitude of uses for this, and I do it all the > time. Additionally, I read somewhere many moons ago (back in my FLASM days) > that declaring variables outside a for loop is less bytecode and uses less > memory. I don't believe that applies to the counter declaration, but I do > know it applies to the comparison as well as vars declared inside the for > loop. However, this level of optimization is only useful in a practical way > on mobile and some games. > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
function doSomething { var i:int; for(i=0;i++;i<10) { } } Is functionally identical to this: function doSomething { for(var i:int =0;i++;i<10) { } } Wrong. It's not. In the latter example, i is not available after the loop. In the first example, it is. var i:int; for (i = 0; i < 10; i++) { if (i == 5) break; } trace(i); -- 5 There are a multitude of uses for this, and I do it all the time. Additionally, I read somewhere many moons ago (back in my FLASM days) that declaring variables outside a for loop is less bytecode and uses less memory. I don't believe that applies to the counter declaration, but I do know it applies to the comparison as well as vars declared inside the for loop. However, this level of optimization is only useful in a practical way on mobile and some games. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
I mean inserting var i:int instead of declaring it outside the loop. On Thu, Mar 27, 2008 at 3:26 PM, Omar Fouad <[EMAIL PROTECTED]> wrote: > Try this: > > for(var i:int; i<10; i++) { >//some crap here > > } > > On Mon, Mar 24, 2008 at 8:49 PM, Kerry Thompson <[EMAIL PROTECTED]> > wrote: > > > jonathan howe wrote: > > > > > Hmm... it is within a class... and that's when I'm getting the > > warnings. > > Or > > > did you mean just in general to reiterate that variables are locally > > scoped > > > to functions and classes and not to for loops? > > > > If you declare a variable within a function, its scope is limited to > > that > > function. It really doesn't relate to where in the function you use it > > (or > > declare it). In my example, this: > > > > function doSomething > > { > > var i:int; > > for(i=0;i++;i<10) > > { > > } > > } > > > > Is functionally identical to this: > > > > function doSomething > > { > > for(var i:int =0;i++;i<10) > > { > > } > > } > > > > You're correct that AS3 is more strict about these sorts of things than > > AS2. > > AS2 was really just syntactic sugar for AS1, and wasn't strict at all. > > > > Cordially, > > > > Kerry Thompson > > > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > > -- > Omar M. Fouad - Digital Emotions > http://www.omarfouad.net > > This e-mail and any attachment is for authorised use by the intended > recipient(s) only. It may contain proprietary material, confidential > information and/or be subject to legal privilege. It should not be copied, > disclosed to, retained or used by, any other party. If you are not an > intended recipient then please promptly delete this e-mail and any > attachment and all copies and inform the sender. Thank you. -- Omar M. Fouad - Digital Emotions http://www.omarfouad.net This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
Try this: for(var i:int; i<10; i++) { //some crap here } On Mon, Mar 24, 2008 at 8:49 PM, Kerry Thompson <[EMAIL PROTECTED]> wrote: > jonathan howe wrote: > > > Hmm... it is within a class... and that's when I'm getting the warnings. > Or > > did you mean just in general to reiterate that variables are locally > scoped > > to functions and classes and not to for loops? > > If you declare a variable within a function, its scope is limited to that > function. It really doesn't relate to where in the function you use it (or > declare it). In my example, this: > > function doSomething > { > var i:int; > for(i=0;i++;i<10) > { > } > } > > Is functionally identical to this: > > function doSomething > { > for(var i:int =0;i++;i<10) > { > } > } > > You're correct that AS3 is more strict about these sorts of things than > AS2. > AS2 was really just syntactic sugar for AS1, and wasn't strict at all. > > Cordially, > > Kerry Thompson > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- Omar M. Fouad - Digital Emotions http://www.omarfouad.net This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
jonathan howe wrote: > Hmm... it is within a class... and that's when I'm getting the warnings. Or > did you mean just in general to reiterate that variables are locally scoped > to functions and classes and not to for loops? If you declare a variable within a function, its scope is limited to that function. It really doesn't relate to where in the function you use it (or declare it). In my example, this: function doSomething { var i:int; for(i=0;i++;i<10) { } } Is functionally identical to this: function doSomething { for(var i:int =0;i++;i<10) { } } You're correct that AS3 is more strict about these sorts of things than AS2. AS2 was really just syntactic sugar for AS1, and wasn't strict at all. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
Hmm... it is within a class... and that's when I'm getting the warnings. Or did you mean just in general to reiterate that variables are locally scoped to functions and classes and not to for loops? I should have mentioned in my subject that this is AS3. So far everyone's alternatives and explanations make sense, I am just curious now as to the reasoning. Either this is a change from AS2 -> AS3 or since AS2 wasn't as strict, it let me do it before. Maybe the explanation lies in the ECMA guidelines somewhere. Anyway, thanks for the discussion, gang. -jonathan On Mon, Mar 24, 2008 at 3:53 AM, Cor <[EMAIL PROTECTED]> wrote: > Using it youre way is possible when you do it within a function or class > because then they are private by default > > Other you could use it this way > Example 1: > > for (var i:int = 0; i < someArray.length; i ++) { > // do something cool > } > > for (i = 0; i < someOtherArray.length; i ++) { > // do something even cooler > } > > --- > Example 2: > var i:int; //or uint if I is never negative > > for (i = 0; i < someArray.length; i ++) { > // do something cool > } > > for (i = 0; i < someOtherArray.length; i ++) { > // do something even cooler > } > > No virus found in this outgoing message. > Checked by AVG. > Version: 7.5.519 / Virus Database: 269.21.8/1340 - Release Date: 23-3-2008 > 18:50 > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- -jonathan howe :: 404.434.2321 :: 180 High St Apt 26 Portland, ME 04101 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
Using it youre way is possible when you do it within a function or class because then they are private by default Other you could use it this way Example 1: for (var i:int = 0; i < someArray.length; i ++) { // do something cool } for (i = 0; i < someOtherArray.length; i ++) { // do something even cooler } --- Example 2: var i:int; //or uint if I is never negative for (i = 0; i < someArray.length; i ++) { // do something cool } for (i = 0; i < someOtherArray.length; i ++) { // do something even cooler } No virus found in this outgoing message. Checked by AVG. Version: 7.5.519 / Virus Database: 269.21.8/1340 - Release Date: 23-3-2008 18:50 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Variable scope within for loops: reusing iteratorvariables
That's easy.only after busting my asss for an hour figuring it out.. ...Variables are only local to the function Jonathan try using j in your second for statementnot the letter i twice Hope that helps... Sent via BlackBerry from T-Mobile -Original Message- From: "jonathan howe" <[EMAIL PROTECTED]> Date: Sun, 23 Mar 2008 20:00:41 To:flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Variable scope within for loops: reusing iterator variables I was hoping someone could explain why I get "Warning: 3596: Duplicate variable definition." warnings when I reuse an iterator variable. Example: for (var i:int = 0; i < someArray.length; i ++) { // do something cool } for (var i:int = 0; i < someOtherArray.length; i ++) { // do something even cooler } I had always thought that the scope of variables declared in the initialization part of the for loop were local to the loop block, not the block containing the loop. So I guess that is not true... does this mean the second time I use the loop I have to omit the declaration? Or is there a keyword I can use to keep the instantiation local to the for block? Seems like it would be clunky if I was cutting and pasting blocks of code in different orders, I'd have to keep track of who was the first for loop of any code block (an admittedly minor annoyance, since I don't have stacks and stacks of loops, but still...) Thanks in advance, -jonathan -- -jonathan howe :: 404.434.2321 :: 180 High St Apt 26 Portland, ME 04101 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders