AS4 Feature proposal: Pass properties by reference

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

AS4 draftActionScript 3 (AS3) supports the idea of being able to pass around a reference to a method. All methods can be treated as an instance of the Function class to facilitate this. Unfortunately, the same is not possible with properties. So whilst one can provide say an event handler with a reference to a private method, the same cannot be done with those frameworks that use properties.

Taking an arbitrary tweening class set as an example, in order to supply the tweening class with details of which property it should tween, one needs to tell it about the subject object and then about a property of that object by passing the latter’s name as a string. The (simplified) code for this class might look like this:

There are various problems with having to do this. The class gets given details of the object and its property as two separate values, which makes validating the information difficult. Only public properties and fields can be used as the validator must access the property via object[name]. Also, as the property is specified as a string, it may not even be a valid property name. These three problems do not apply to methods passed by reference, they occur due to the need to reference a property as a name in a string.

So this AS4 proposal is that it be possible to pass references to properties. Just as methods can be referenced via instances of the Function class, so properties could be referenced via instances of a new Property class. The Property class’ definition might then be something like:

Such a feature would allow the tweening class to be rewritten thus:

As well as the obvious fact that this simplifies the code, it has other advantages too. The constructor can simply validate that the supplied property has the right type and can be read from and written to. Doing this with reflection and try/ catch blocks in the case of reference by name in string is very complex in comparison. Perhaps the most important benefit though is that the property need not be public. It can be protected or internal (or if you really must, private), yet the tweening class can still access it. This is impossible using name strings.

So far, I’ve only considered the receiving of a property reference. To complete the feature, it of course has to be possible to send a reference too. To send a reference to a method is of course easy. As any method must take the form:

a reference to it can be inferred by simply using “name”. With properties though, “name” by itself is the syntax for referencing its value. Borrowing from C/C++, there is a simple semi-standard way of passing by reference:

So if I have a property, a, that I wish to tween, I’d create a tween class instance thus:

And to my mind, the killer argument for this feature is that property “a” could be a private one and things would still work.

UPDATE
I have responded to Paul Robertson’s suggestion by posting details of this idea on Adobe Labs Ideas and Adobe’s ActionScript JIRA.

Posted in AS4

8 thoughts on “AS4 Feature proposal: Pass properties by reference

  1. Doesn’t this break encapsulation, especially if you can pass private variables? Also, I’d imagine it’d suck for performance. Perhaps the mid ground is to allow passing of accessor methods?

  2. @Owen,

    Quite the opposite: it encourages encapsulation. Currently if I want say a Validator or Tweener to access a property, I have to make it public. By being able to pass by reference though, I can give those classes selected access to protected or private properties.

    And I don’t see why there should be any performance hit over boxing a method’s address up in an instance of Function and passing that around.

  3. @Paul,

    It wasn’t available on either, but it is now. Thanks for the suggestion. See the update at the end of the post for the URLs πŸ™‚

  4. I really like the idea. πŸ™‚

    So, if I understand you correctly, the current way of getting or setting values will still remain, but prefixing a property with ampersand would “typecast” (sortof) it as a Property. Something like this:
    getQualifiedClassName(this.x); // Number
    getQualifiedClassName(this.&x); // Property

    I like it! It would be a lot more typed, and would likely be handled by the compiler, which means compile time checking for errors rather than runtime.

    Where did you get the idea from (other than the obvious current need for it!) Is there any language that uses this style of keeping track of properties already?

    How about also suggesting some Vector style typing for the property? (so that passing in and trying to tween a String or Sprite wouldn’t work)
    public function Tween(property:Property.)

    If no type is specified (like in your example), it’s assumed that the property is of type “Property.”, meaning any property can be passed through.

  5. Sorry, it seems like HTML tags are stripped out. That was supposed to be:
    public function Tween(property:Property.<Number>)

    And if no type is specified, it is assumed the property is of type:
    Property.<*>

  6. Hi Andreas,

    As far as I know, it’s an original idea. It got frustrated last week over having to make a property public just so a custom validator class could access it. Having spent far too many years programming in C, the syntax seemed the natural choice to me πŸ™‚

    You are right, this.x would yield a Number and this.&x would yield a Property.

    I completely agree with you that adding generics (ie the “Vector style typing”) would be the icing on the cake. Whilst there are many enhancements I’d like to see for ActionScript, generics remains top of my list.

  7. @owen. Can’t see how it breaks encapsulation, you pass a private method to an EventDispatcher with out thinking about it.

    I was wondering how this might affect the whole event listening / binding set up. I know the old as3 watch / unwatch had a high overhead.

Comments are closed.