E4X in ActionScript

Looking at the implementation of E4X in ActionScript, it occurs to me that they need to figure out how to do three things, which I'm not sure what can be done in ActionScript regularly:

Properties / Getters prefixed with @: var myAttribute = xmlPerson.@name;

Unnamed functions for filtering: xmlData.person.(/* predicate */)

lambda syntax for predicates: xmlData.person.(@name == "Brian")

So, here is my question: are these only one-time features (like, for example Vector.<>) that they insert only for E4X, so before they reach us? Or do we, as ActionScript developers, have access to these features?

In particular, I would like to access the expression tree of this lambda predicate for my own code (not attached to XML classes in any way).

I realized this is called a "filter statement" ... but I'm not sure how to use it. Not sure I can ... because ActionScript does not allow operator overloading :(

+5
source share
3 answers

As far as I know, it is impossible to use the E4X syntax for other types of objects. This is very sad because it has a lot of potential; especially the lamdba syntax.

+1
source

I think you need a proxy class. The mechanism of this class is quite interesting, which allows processing dynamic access to non-existent, but algorithmically defined properties. To do this, you subclass the flash.utils.Proxy class.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Proxy.html

/ . (@) . - - @, . :

package {
    import flash.display.Sprite;

    public class ProxyTest extends Sprite {
        public function ProxyTest() {
            var obj:ProxyObject = new ProxyObject();

            trace(obj.testingDefault); // Invokes default logic.
            trace(obj.@testingAttributes); // Invokes default logic. :(
            trace(obj["(@testingLambda == 'testing')"]); // Invokes lambda logic.
            trace(obj.filter("@testing == 'testing'")); // Just an idea

            trace(obj.(@testing = "testing")); // Throws Error #1123: Filter operator not supported 
        }
    }
}

import flash.utils.Proxy;
import flash.utils.flash_proxy;

dynamic class ProxyObject extends Proxy {

    public function ProxyObject() {
    }

    override flash_proxy function callProperty(propName:*, ... args):* {
        var name:String = propName.toString();

        switch (name.charAt(0)) {
            case '(':
                trace("Lambda logic.");
                break;
            case '@':
                trace("Attribute logic.");
                break;
            default:
                trace("Default logic.");
                break;
        }

        return "CALL: " + propName;
    }

    override flash_proxy function getProperty(propName:*):* {
        var name:String = propName.toString();

        switch (name.charAt(0)) {
            case '(':
                trace("Lambda logic.");
                break;
            case '@':
                trace("Attribute logic.");
                break;
            default:
                trace("Default logic.");
                break;
        }

        return "GET: " + propName;
    }

    override flash_proxy function setProperty(propName:*, value:*):void {
    }

}
+1

I know I'm late, but Brain, it seems like filter operators are supported in AS3 !!

Check out this error I received:

TypeError: Error #1123: Filter operator not supported on type XYZ

In this line of code:

object.(getChildByName("image") as Sprite).addChild(img);

Well .. I guess we'll find out the rest ...

+1
source

All Articles