Thanks Troy,
 
I reckon a series of custom collection classes is where it is going.
String equality is not good enough, and strict equality is often not
what is required.
 
Hmmmm.
 
Pity 'Object' doesn't implement equals() and hashCode() like Java eh.
(or does it somehow?)
 
Greg.

 
  _____  

From: Troy Gilbert [mailto:[EMAIL PROTECTED] 
Sent: Friday, 15 June 2007 3:09 AM
To: [email protected]
Subject: Re: [flexcoders] A Question of AS3 Object Equality and
Collections



I expect that arrays use strict equality, hence the results you're
seeing. In fact, I would assume all collections to use strict equality.
I think the only reason that there's a distinction in Object and
Dictionary is because Object *always* uses strings as keys, and thus
calls toString() on non-String objects, and thus gets a quasi equality
going on... hence the need for Dictionary which basically just never
calls toString(), resulting in the revelation that its strict equality
only. 

Am I making any sense?

So, to answer your question, I'd guess a custom collection class would
be the appropriate fix. I've run into it several times (like an ordered
collection with sparse unique ID's) where you basically have to wrap an
Array with all the appropriate access API. 

Troy.



On 6/14/07, Greg McCreath <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote: 

        

        Hi All,
         
        In AS3 we have only the Object, Array, and Dictionary to use for
collections (as far as I know!).  I'm posting this unless I am missing
something about how AS3 handles equality in collections.  I'm using
QName objects but I guess anything could do.  
         
        Consider the following:
         
        var nameSpace1: Namespace = new Namespace("http://foo
<http://foo> ");
        var nameSpace2: Namespace = new Namespace("http://bar
<http://bar> "); 
         
        var qname1: QName = new QName(nameSpace,"bob");
        var qname2: QName = new QName(nameSpace,"bob");
        var qname3: QName = new QName(nameSpace2,"bob");
                    
        trace("are equal1 :" + (qname1 == qname2));
        trace("are equal2 :" + (qname1 === qname2));
        trace("are equal3 :" + (qname1 == qname3));
        
        Gives : 
         
        true
        false
        false
         
        Good.  AS3 believes qname1 & qname2 are equal, and qname2 and
qname3 are not.  Excellent.  Good start.
         
        However: 
         
        var a: Array = new Array();
        a.push(qname1);
        trace(a.indexOf(qname2) > -1);
        
        
        Ooops.  They are not quite the same after all.  So, with this
behaviour different part of my framework cannot create a QName and put
in an Array for other classes to get out with their own QName objects -
even though AS3 says they are equal.
         
        Perhaps I could use a Dictionary.  However it is based on '==='
strict equality and doesn't fit the bill because of that.  In fact, try
adding a QName as a key to a Dictionary like:
         
        var nameSpace: Namespace = new Namespace("http://foo";);
        var qname: QName = new QName(nameSpace,"bar");
        var d:Dictionary = new Dictionary();
        d[qname] = "why does this blow up?";
        
        gives an explosion:
         
        ReferenceError: Error #1056: Cannot create property
http://foo::bar on flash.utils.Dictionary.
         
        
        
        you simpy cannot use a Dictionary object for QNames.  I've tried
it in 10 different ways (namespaces with/without prefixes etc etc). I'll
post this separately I reckon.
         
        Bottom line: Do I have to create my own collections classes (a
la Java) and use my own equality mechanisms... ?
         
        All replies appreciated.
         
        Greg.
         

        

  _____  

        This email and any files transmitted with it may be confidential
and are intended solely for the use of the individual or entity to whom
they are addressed. This email may contain personal information of
individuals, and be subject to Commonwealth and/or State privacy laws in
Australia. This email is also subject to copyright. If you are not the
intended recipient, you must not read, print, store, copy, forward or
use this email for any reason, in accordance with privacy and copyright
laws. If you have received this email in error, please notify the sender
by return email, and delete this email from your inbox. 

        

        

        


 


This email and any files transmitted with it may be confidential and are 
intended solely for the use of the individual or entity to whom they are 
addressed. This email may contain personal information of individuals, and be 
subject to Commonwealth and/or State privacy laws in Australia. This email is 
also subject to copyright. If you are not the intended recipient, you must not 
read, print, store, copy, forward or use this email for any reason, in 
accordance with privacy and copyright laws. If you have received this email in 
error, please notify the sender by return email, and delete this email from 
your inbox. 

Reply via email to