Collection & Iterator and Class(cast)

I learned how to utilize the Collection and Iterator patterns yesterday, thanks to Dan, the Java head (head of Java) at my job. I had previously asked Kenny B awhile ago, and he had explained it just as thoroughly, but typecasting to interfaces was a fuzzy subject, as was why one would do such a thing.

Additionally, so satisfy Google keyword searchs for said topic since the ones I found were pathetic, I hope to create a new tutorial this weekend on AMFPHP and utilizing the mx.utils.Collection, mx.utils.CollectionImpl, mx.utils.Iterator, and mx.utils.IteratorImpl. Contrary to the docs, the whole dragging of an SWC file to get the classes file to work doesn’t work; I tried. You have to get the files from the AS2 Remoting Source files download, as mentioned at Rich-Internet.

The only roadblock I’m having with them is I can’t get my authoring component to work. The code itself works beautifully, and I’m overjoyed at how neat Collections and Iterators are; you can do a crapload more than one could do using arrays and looping through them with far less code. However, following the docs to the T, I still couldn’t get my component properties inspector to show anything. If you know, let me know.

Secondly, something I saw in Moock’s book and in the docs which baffles me is the whole:

var value:Class = Class(something);

Now, I know do this to avoid a type-mismatch error:

var i:Number = 1;
var o = String(i);
var str:String = new String(o);
trace(str); // 1
trace(typeof(str)); // object
trace(str instanceof String); // true

The conversion of i to a string via using the String class constructor makes sense, as does Number (although, I recommend parseInt). But when the docs do like:

var button:ButtonC = ButtonC(someObj);

That's weird... it doesn't work. In the example they give for CompactDisk, the constructor does not do any initialization at all. Maybe it works in other languages but you cannot assume that doing the above will work like an attachMovie initialization object, and simply set the class's member variables to the value passed in; it only works for Intrinsic classes OR ones you've specifically wrote yourself. By Intrinsic, I mean:

s = String(num);
num = Number(s);
d = new Date(2004);
a = new Array("mic", "check");

But if you make a class CompactDisc, and don't do squat with your constructor, passing in an object into CompactDisc will NOT return a valid object. My example didnt' work till I did this myself:

function MyCollectionDataConstructor()
if(arguments[0] != null)
for(var p in arguments[0])
this[p] = arguments[0][p];

When I did that, she worked like a charm. My guess is, in Java or some other language, this just works:

var val:NewClass = NewClass(o);

Where o is some object with data, not of class NewClass, and by doing the above it returns a converted instance or something... whatever man, not in Flash. Do the above, and you get the same effect, though.

6 Replies to “Collection & Iterator and Class(cast)”

  1. Hey, it’s a big step in the right direction when MM start supplying Iterators and the like, shame it had to come later on in the AS2 remoting doo das. The RecordSetIterator can lend itself to making very tight code when it comes to your AMFPHP stuff!

    Next big hurdle, remove the damn mix-ins, they feel dirrty (specifically pointing the finger at the extensions added to Array with DataProvider), I dunno maybe it’s me, or maybe it’s just makes Flash sense for efficiency reasons. But maybe its also why half the intrinsic classes don’t need abstract AS files created to satisfy the compiler (Array), and the other half do?

    I’ve just tried out your findings, I couldn’t believe another oddity had been found. When you pass in say an Object instead of a CompactDisc to a function, and cast it as CompactDisc, you get ‘null’ back (meaning the casting has failed in Flash Player 7), not a CompactDisc (even tho Flash Player 6 does still return the Object rather than null)… even if their structure is indeed identical. However on a slightly seperate note, it does say in the live docs:

    You can’t override primitive data types that have a corresponding global conversion function with a cast operator of the same name

    So there’s the deal we get, ‘Array()’ etc are built in conversion functions. Any other ‘Class()’ is merely casting. As for casting an Object to SomeSubClassOfObject, it’s only there to assert, not to convert even tho the assertion fails silently in certain circumstances. :(

    Look forward very much to your AMFPHP tutorial! Will you post it up on your blog?’

  2. var value:Class = Class(something);

    This is just a ‘trick’ to tell the compiler to treat ‘something’ as an instance of Class. It’s called casting.

    To quote Moocks EAS2 :’ Casting is used to tell the compiler the type of an object when the complier can’t determine the object’s type on its own’

  3. I am having the same problem with component props not showing up in the inspector too, and I saw the description for this page. But I am not seeing any text here…???


Comments are closed.