Constants

I will be releasing this class as part of my Dev Kit, but since the source for that won’t be out for a while, i figured I’d show you guys this…

It’s really simple as of right now, doesn’t throw any errors if a you try to set a value to a constant.

Well since Flash has no native support for Constants, I went ahead and wrote a class that will transform a normal variable into a constant…

here’s the class

saved as, Constant.as


/**
 * Since Flash has no support for Constants, I offer this to make constants.
 *
 * @author Michael Avila
 * @version 1.0.0
 */
 class Constant
 {
    /**
     * Makes any variable that you send a constant.
     * This means that if you try to set it after it has been made
     * a constant, it will still be the same value as when it was initalized.
     *
     * @param The object that the variable you wish to make a constant, resideds in.
     * ...
     * @param The string version of the name of the variable
     *
     */
    public static function makeFinal( object:Object, varName:String ) : Void
    {
       function keepConstant(prop:String, oldVal:Object, newVal:Object, object:Object)
       {
           return oldVal;
       }
       

       object.watch(varName, keepConstant, object);
    }
 }

You will find this in the Dev Kit in the package…

createage.actionscript

Here is a very simple example of the code used in an fla…


var myObj = new Object();
myObj.myVar = 5;

Constant.makeFinal(myObj, "myVar");
myObj.myVar = 6;

trace(myObj.myVar);

Take Care, guys and girls.

_Michael

can you post a swf?

wow, nice thanx man. Good contribution to k-land.

There’s nothing that an swf can do to show you this… it’s just a coding thing.

Thanks for the reply Defective, I try… lol… check out my Dev Kit I’m working on…

Documentation:
http://www.createage.com/CreateageLib

_Michael

That’s pretty hot :thumb:

Thanks thanks, I’m working on making it throw a ConstantValueException.

_Michael

Um… why would you want to? Just throw the exception, and don’t try returning it. That wouldn’t make any sense.

well the problem is that the Exception won’t cause an error at compile time… and for the sake of not completely murdering the application if I could return the old value and still throw the Exception…

The throwing an error alone is pretty much my only choice at this point… I’m not sure how I’m going to go about this one yet.

_Michael

Well… you should be putting anything that might throw an exception in a try catch (unless you also intend to throw it up higher), so, I still don’t see the problem.

Your class sure seems like a lot more work than just not changing the value in the first place. Best practices say to keep constants in all caps so you know not to change the value. No offense, I just don’t see any purpose for this class.

The logic behind what you’re doing seems to be moving toward a Singleton design pattern where a class can only be instantiated once: http://en.wikipedia.org/wiki/Singleton_pattern

I’m 100% aware of what the Singleton pattern is. Putting constants in all caps is good practice and leads to more readable code, but it does not make them constants, you are still able to change their value. The real benefits are met when you are distributing code… if you have a class that you don’t want the value of to be changed, or if you feel that you need to take every precaution to make sure this variable does not get changed, then this class is useful.

_Michael

I guess we’ll just agree to disagree. Sorry if it came off as an attack (I’m not used to the kirupa crowd even knowing what a class, let alone a design pattern, is). Cheers.

(By the way, nice looking blog you’ve got).

lol, I didn’t take it harsh at all, you were just making a suggestion, which I completely respect you for :).

_Michael

Or you could use ASSetPropFlags:

var obj:Object = new Object();
obj.numeric = 123;
obj.alpha = "abc";
obj.boolean = true;
ASSetPropFlags(obj, ["numeric", "alpha"], 6);
trace(obj.numeric);//123
trace(obj.alpha);//abc
trace(obj.boolean);//true
obj.numeric = 321;
obj.alpha = "cba";
obj.boolean = false;
trace(obj.numeric);//123
trace(obj.alpha);//abc
trace(obj.boolean);//false

See:
http://www.flashguru.co.uk/assetpropflags/
http://labs.blitzagency.com/?p=59

Or get/set functions:

class Math2 {
 public static function get PI2():Number {
  return Math.PI * 2;
 }
 public static function set PI2(Void):Void {
 }
}
//__________//
trace(Math2.PI2);//6.2 something
Math2.PI2 = Math.PI;
trace(Math2.PI2);//6.2 something

:hoser:

Besides all the discussion of wheter the class is useful or not, i think the solution Michael found suits his needs, and most people dont even know that Object.watch can be used to prevent value changes of a variable.

:thumb:

I think the fact that almost every programming language has constants is a good indication that this class is useful. I was surprised when I started coding AS that it didn’t

You won’t have to be surprised for long, you can define constants in AS 3.0:
http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#const

I cannot wait until AS 3.0 is practical in Flash development… it’s going to be forever, AS 2.0 is barely starting to be ok in production use.

_Michael

well in my opionion this is a worthless class and shoulnd’t even be used to take up space in my swf, its easy enough to just make a simple function in my project which will achieve this goal, anyhow if it helps you out or anyone else thats cool but to me its a waste of time especially since i don’t usually change constants once i set them

Once again, the befefits are met when Distributing code… it’s easy to keep yourself from changing a constant, but you can not trust other people that will be using your code.

If you do not distribute code to others in any way, your right, this class may not be useful.

_Michael