Here's a little tidbit that will be old-hat to any Flex/Flash people out there, but as a CF guy, I found it pretty interesting. Most ColdFusion programmers have probably heard of implicit getters and setters as a pattern some have adopted in CFML (and others seem to vehemently oppose). While learning Flex, I have found that ActionScript 3 also supports implicit getting and setting but it means something completely different that what I had expected.
In ColdFusion, the point of implicit getters and setters (As they are colloquially referred to) generally seems to be an attempt to leverage the dynamic flexibility of the language to reduce boiler plate code and find the most streamlined way (code-wise) to still retain the encapsulation of get and set methods to access private properties of an object. There are two main ways implicit gets and sets manifest themselves in ColdFusion.
OnMissingMethod()In this approach, the OnMissingMethod method is employed to handle method names like getPropertyName() where "PropertyName" equals or maps to an internal property. I have an example of such a method in my experiments with Synthesized Objects that is used something like this:
2 myObject = createobject(...)
4 foo = myObject.getProperty1();
Generic get() and set()This approach is similar in the previous in that is uses a single generic method to handle multiple properties. It requires a generic get method and a generic set method which allow for the name of the property in question to be passed in along with a value in the set case. They usually look something like this:
2 <cfargument name="property">
3 <cfreturn variables[arguments.property]>
6 <cffunction name="set">
7 <cfargument name="property">
8 <cfargument name="value">
9 <cfset variables[arguments.property] = value>
And usage looks like this:
2 myObject = createobject(...)
4 foo = myObject.get("property1");
Note, both of these methods often use either a list of acceptable properties or cfproperty Meta data to control what can be set and retrieved. Both of these methods also are an attempt to not be required to write each get and set individually, but still use them.
How ActionScript Does ItImplicit getters and setters in ActionScript are sort of the opposite. They require you to still write our all your get and set methods, but you get to use them without looking like you are calling a method. In other words, your code has the natural appearance of directly accessing properties even though getters and setters are actually being called in the background.
Usually people name the getters and setters the same as their property name (or at least the way they want it to be externally accessed). The get and set methods are allowed to share the same name, but the property itself has to be called something different. Usually an underscore is appending to the beginning of the private variable name. This is what it looks like:
3 var private _property1:String;
5 public function get property1():String
7 return _property1;
10 public function set property1(value:String):Void
12 _property1 = value;
Note the extra "get" and "set" keywords before the method name. This is how they are used:
2 myObject.property1 = 'foo';
3 var bar:String = myObject.property1;
It would appear we are directly accessing the property, but in reality it is being handled by the getter and setter method. The programmer isn't saving on any lines of code per se, but I do like the way it can un-clutter the code when doing a lot of gets and sets. Especially when nesting gets such as:
as opposed to
On a side note, Railo is reported to already allow for this functionality if I understand correctly under the name Magic Functions but I have yet to try it out. Perhaps ColdFusion 9 will allow for something like this.