ActionScript 3 Wildcard Setters

All credit goes to Tyler for this trick, but I’m sure I would have thought of it too. :)

Tyler has been working on an XML specification, what he things Adobe should have done in place of FXG which is a standard that was built around implementation with Flex. During this process he has also done tests from time to time to see how it might actually pan out using Flex builder, and we’re pretty confident it could all be done there. One of the issues is this new standard should support shortcuts. An example in the XML is:

    <layout padding="10px 10px 10px 50%"/>
<group transform="rotate(30)"></group>

(note: this xml is not necessarily from the actual specification)

This is impossible with current Flex implementations if you want your properties typed to their type. For example, the padding property may be an Bounds object with top, left, right, and bottom properties of type Length which could be a hard value or a percent.

What he found though was that you can have wildcard setters with your strictly typed getters. This allows you to set anything to the property, but when you get it you expect the proper type (and get compile-time errors, autocomplete, etc.). This allows us to take a string of “10px 10px 10px 50%” or “rotate(30)” and parse it into the proper type.

public function get padding():Bounds
    return _padding;
public function set padding(value:*):void
    if ( !(value is Bounds) ) {
        // convert it to a Bounds object
        value = ValueUtils.parseBounds(value);

    if (value.equals(_padding)) {

    _padding = value;
    dispatchEvent(new Event("paddingChanged"));

This will allow the XML specification to continue being defined without regards to the Flex compiler and we can also feel confident that it will still work with Flex. Now we can have our cake and eat it too!

This may also be a cool trick in other scenarios than just MXML. But think of all the shortcuts you wished you could do and how this makes it possible while still keeping good standards and typing your variables.

3 Responses to “ActionScript 3 Wildcard Setters”

  1. Juan Carlos Says:

    Hi… sorry for posting here but couldn’t find the right place. Found a couple of bugs on “date format” (javascript’s php date format simulation proyect):

    Original ‘h’ parsing:
    h: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? ‘0’ : ”) + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() – 12); }

    I changed it to:
    h: function() { return ((this.getHours() 12 && this.getHours() < 22)) ? ‘0’ : ”) + (this.getHours() <= 12 ? this.getHours() : this.getHours() – 12); },

    Again, sorry for posting in here. Thanks for your work.

  2. Jacob Wright Says:

    Thank you. I went in to fix it and realized an easier way to do that format (and ‘g’) to be even smaller using the modulus operator. Sorry for the bug. It has been updated on the javascript date format page.

  3. An Alternative to using PropertyChangeEvent - Jacob Wright – Flex, AIR, PHP, etc. Says:

    […] wildcard setters and turn String values of width and height into pixel values (e.g. 2cm, […]