private
Array variable within a class definition, implementing external access to it as a read-only getter. In JavaScript, you could implement a closure within your object structure that would return the set of values as an Array object. However, both of these implementations have a number of holes, assuming you want your set of values to truly be constant.The Array objects in both variants of ECMAScript-262 are mutable, meaning that they are subject to change at runtime. Additionally they are objects and thus are passed as pointers to their place in memory, meaning that any changes made on the arrays will ripple throughout your code, affecting any code storing a reference to your "constants".
Immutability
When you truly want something to be constant, you want that something to not only never change but be impossible to change at runtime. Collections in ActionScript and JavaScript are mutable by their very nature: they are designed to change. Collections are sorted, the keys and values are shifted, pushed, and deleted, etc. Sure, you can define a variable like the following:
const MY_COLLECTION = [1,2,3,"bob"];
However, at runtime, any references to
MY_COLLECTION
can move 2
before 1
, delete "bob"
, etc. Not good if you intended the collection to be constant, and not good if you don't want the changes in the collection to ripple throughout your code base.How do we achieve the intended results? By using an immutable type to store the value.
Strings
So we need an immutable type. Strings are immutable. This means that they do not change. You might be asking, "Wait a second. I can change a string. I can call
substr()
, push new characters on the string, concatenate it, etc. Strings can change." Actually they can't.Strings are not only value types, but are also immutable. When you update a string by concatenating another string to it, you are actually creating a new string from the two concatenated substrings. This substring is then placed in memory at the location of the old string, making it appear that the string has changed. The string itself has not changed, it has been replaced.
This means they make effective constants. You use string constants all the time in ActionScript:
Event.EVENT
is one common example, used in event addition and handling. Defining a usable collection constant is very easy as well: use a string.Using Data Transfer Formats As String Constants
A simple string can't act like a collection can it? Ever heard of CSV? It stands for comma separated value, and it is a data transfer format. Data transfer formats are useful formats for encoding objects into a transportable form. In the case of a collection, we're talking about encoding a list of values into a string.
For simple collections of value primitives, CSV formatted strings work quite well:
const MY_COLLECTION = "1,2,3,'bob'";
A CSV doesn't have to be comma-separated, though. If you want your values to be strings that contain commas, you can use spaces, tabs, underscores, exclamation marks, separator tokens ("%sep%"), special unicode characters (\u10225), etc. As long as you separate each string with the same token, all you need to do to turn the string into an array when you want to use it is to call
split()
on the string, passing your separator token as the parameter. This will return the collection.I particularly like using CSV constants when I need an enumeration of a few values, like "cat", "dog" or "turtle" for a list of possible pets.
const PETS = "cat,dog,turtle";
This type of enumeration comes in handy when I'm validating entries in setters. For this example, I want to make sure that the value for the pet setter is either dog, cat, or turtle. Wouldn't want any rats or roaches for pets.
public static const PETS = "cat,dog,turtle";
private var _pet:String;
public function set pet(value:String):void{
if(PETS.indexOf(value) !== -1){
_pet = value;
}
}
You can use any of the
String
object methods on your collection constant. Here I used the indexOf()
method to check to make sure the value was valid.CSVs may not cut it if you are using more complex objects. In those cases you can use other data transfer formats, like XML, JSON, or YAML. Just be sure to use the string notation of these formats, so that they are immutable. These types of constants can be rehydrated throughout your applications and libraries where they need to be used, and the resulting objects can even be changed, without affecting other portions of your application that use the same constant.
Happy coding!