Loop Safety
This is a curiosity I’ve had for far too long. Why didn’t I make these simple tests years ago when I was first learning AS3? I’m not sure, but judging by a lot of other people’s AS3 that I’ve read, many people don’t seem to understand it.
What does this print?
trace("before"); for (var i:* in null) { trace("inside"); } trace("after"); trace("before"); for each (var j:* in null) { trace("inside"); } trace("after");
Does the loop crash on the null when it tries to iterate over it? Would it necessitate the kinds of null checking I’ve seen so often in AS3 code? Nope! The for-in and for-each loops conveniently and efficiently skip the loop and we get:
before after before after
On that happy note, enjoy your weekend!
#1 by golgobot on July 10th, 2009 ·
That got me to thinking.
What do you think the following code would do?
Well apparently it’s all valid. Here’s the output.
dict[null] = null
dict[undefined] = undefined
dict[NaN] = NaN
dict[[class int]] = int
j = null
j = undefined
j = NaN
j = int
#2 by jackson on July 10th, 2009 ·
This is actually in my notes for an upcoming post. I will cover some details of dictionary keys. For example, what kinds of type conversion do you think it does? Consider this:
#3 by golgobot on July 10th, 2009 ·
on that note what would this code trace out?
When you actually run this code, you’ll be surprised, but not taken aback.
#4 by jackson on July 18th, 2009 ·
Thanks for this. It’s the subject of post for Friday the 24th.