Monday, 24 July, 2006

ActionScript 3: No pass by reference

When you first start learning a new development platform, the learning curve looks more like a learning cliff:  you have to assimilate a huge amount of information up front in order to become even marginally productive.  My current knowledge of ActionScript 3 is about where my knowledge of .NET was four years ago.  I'm just barely able to put together a working program.

Today I was wondering how to pass parameters by reference in ActionScript 3, and came across an odd design decision: all parameters are passed by value.  I know that the documentation says that objects are passed by reference and primitives are passed by value, but in reality everything's passed by value.  An object is a reference, so what you're really passing to a function is a pointer.  In this respect, ActionScript is like C except that in C you can pass a pointer to a value type.

Nomenclature notwithstanding, the odd design decision here is that it's impossible for a function to change the value of a primitive type (Boolean, Number, int, uint, and String) that's passed as a parameter.  The function can change the value locally, but the global copy of the parameter is not affected.  Ever.

Exceptions to rules make for confusion, and this is one thing that's going to confuse a whole lot of programmers.