<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Enums and ActionScript&#039;s Static Initializers</title>
	<atom:link href="http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/</link>
	<description>Thoughts, rants, and even some code from the mind of Barney Boisvert.</description>
	<lastBuildDate>Thu, 11 Sep 2014 09:58:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Glen Flint</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-388669</link>
		<dc:creator>Glen Flint</dc:creator>
		<pubDate>Fri, 12 Jul 2013 21:39:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-388669</guid>
		<description>Thanks for your informative post!  Here&#039;s an enumeration class that draws upon you insights...

&lt;pre&gt;package common
{
	import flash.utils.Dictionary;

	public class Enumeration
	{	
		private var type:String;
		private var name:String;
		
		private static var initialized:Dictionary = new Dictionary();
		
		private static var inOrder:Dictionary = new Dictionary();
		
		private static var byName:Dictionary = new Dictionary();
		
		public function Enumeration(type:String, name:String)
		{		
			if (initialized[type] != null) {
				throw new Error(type + &quot; is an enumeration.  New is not allowed.&quot;);
			}
			
			this.type = type;
			this.name = name;
			
			storeInOrder();
			
			storeByName();
		}
		
		private function storeByName():void {
			if (byName[ this.type ] == null) {
				byName[ this.type ] = new Dictionary();
			}
			
			byName[ this.type ][ this.name ] = this;
		}
		
		private function storeInOrder():void {
			if (inOrder[ this.type ] == null) {
				inOrder[ this.type ] = new Array();
			}
			
			inOrder[ this.type ].push(this);
		}
		
		public function toString():String {
			return this.name;
		}
		
		protected static function getAll(type:String):Array {
			if (! initialized[type]) {
				throw new(type + &quot; not initialized yet!&quot;);
			}
			
			return inOrder[ type ].concat();   // copy so no one can mess it up
		}
		
		protected static function fromString(type:String, name:String):Enumeration {
			if (! initialized[type]) {
				throw new(type + &quot; not initialized yet!&quot;);
			}
			
			if (byName[ type ][ name ] == null) {
				throw new Error(type + &quot; name unrecognized: &quot; + name);
			}
			
			return byName[ type ][ name ];
		}
		
		protected static function initialize(type:String):void {
			initialized[type] = true;
		}
	}
}
&lt;/pre&gt;

Here&#039;s some code to test it...

&lt;pre&gt;package suite.cases.common
{
	
	import flash.errors.IllegalOperationError;
	
	import org.flexunit.Assert;
	import org.flexunit.assertThat;
	
	public class EnumerationTest
	{		
		[BeforeClass]
		public static function runBeforeClass():void {   
			// run for one time before all test cases   
		}   
		
		[AfterClass]  
		public static function runAfterClass():void {   
			// run for one time after all test cases   
		}   
		
		[Before(order=1)]
		public function runBeforeEveryTest():void {   
			// run before every test				
		}
		
		[After]  
		public function runAfterEveryTest():void {   
			// run after every test
		} 		
		
		[Test]  
		public function testToString():void {		
			Assert.assertEquals(&quot;Apple&quot;, Fruit.APPLE.toString());	
			Assert.assertEquals(&quot;Banana&quot;, Fruit.BANANA.toString());
			Assert.assertEquals(&quot;Orange&quot;, Fruit.ORANGE.toString());
		}
		
		[Test]  
		public function testFromString():void {		
			Assert.assertEquals(Fruit.APPLE, Fruit.fromString(&quot;Apple&quot;));	
			Assert.assertEquals(Fruit.BANANA, Fruit.fromString(&quot;Banana&quot;));
			Assert.assertEquals(Fruit.ORANGE, Fruit.fromString(&quot;Orange&quot;));
		}
		
		[Test]  
		public function testGetAll():void {		
			Assert.assertEquals( 
				[ Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE ].toString(),
				Fruit.getAll().toString());										
		}
		
		[Test(expects=&quot;Error&quot;)]  
		public function anotherFruit():void {		
			new Fruit(&quot;PLUM&quot;);										
		}
	}
}

import common.Enumeration;

class Fruit extends Enumeration {
	
	private static const FRUIT:String = &quot;Fruit&quot;;
	
	public static const APPLE:Fruit = new Fruit(&quot;Apple&quot;);
	public static const BANANA:Fruit = new Fruit(&quot;Banana&quot;);
	public static const ORANGE:Fruit = new Fruit(&quot;Orange&quot;);
	
	Enumeration.initialize(FRUIT);		
	
	public function Fruit(name:String) {		
		super(FRUIT, name);
	}
	
	public static function getAll():Array {
		return Enumeration.getAll(FRUIT);
	}
	
	public static function fromString(name:String):Fruit {		
		return Enumeration.fromString(FRUIT, name) as Fruit;
	}
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Thanks for your informative post!  Here's an enumeration class that draws upon you insights&#8230;</p>
<pre>package common
{
	import flash.utils.Dictionary;

	public class Enumeration
	{
		private var type:String;
		private var name:String;

		private static var initialized:Dictionary = new Dictionary();

		private static var inOrder:Dictionary = new Dictionary();

		private static var byName:Dictionary = new Dictionary();

		public function Enumeration(type:String, name:String)
		{
			if (initialized[type] != null) {
				throw new Error(type + " is an enumeration.  New is not allowed.");
			}

			this.type = type;
			this.name = name;

			storeInOrder();

			storeByName();
		}

		private function storeByName():void {
			if (byName[ this.type ] == null) {
				byName[ this.type ] = new Dictionary();
			}

			byName[ this.type ][ this.name ] = this;
		}

		private function storeInOrder():void {
			if (inOrder[ this.type ] == null) {
				inOrder[ this.type ] = new Array();
			}

			inOrder[ this.type ].push(this);
		}

		public function toString():String {
			return this.name;
		}

		protected static function getAll(type:String):Array {
			if (! initialized[type]) {
				throw new(type + " not initialized yet!");
			}

			return inOrder[ type ].concat();   // copy so no one can mess it up
		}

		protected static function fromString(type:String, name:String):Enumeration {
			if (! initialized[type]) {
				throw new(type + " not initialized yet!");
			}

			if (byName[ type ][ name ] == null) {
				throw new Error(type + " name unrecognized: " + name);
			}

			return byName[ type ][ name ];
		}

		protected static function initialize(type:String):void {
			initialized[type] = true;
		}
	}
}
</pre>
<p>Here's some code to test it&#8230;</p>
<pre>package suite.cases.common
{

	import flash.errors.IllegalOperationError;

	import org.flexunit.Assert;
	import org.flexunit.assertThat;

	public class EnumerationTest
	{
		[BeforeClass]
		public static function runBeforeClass():void {
			// run for one time before all test cases
		}   

		[AfterClass]
		public static function runAfterClass():void {
			// run for one time after all test cases
		}   

		[Before(order=1)]
		public function runBeforeEveryTest():void {
			// run before every test
		}

		[After]
		public function runAfterEveryTest():void {
			// run after every test
		} 		

		[Test]
		public function testToString():void {
			Assert.assertEquals("Apple", Fruit.APPLE.toString());
			Assert.assertEquals("Banana", Fruit.BANANA.toString());
			Assert.assertEquals("Orange", Fruit.ORANGE.toString());
		}

		[Test]
		public function testFromString():void {
			Assert.assertEquals(Fruit.APPLE, Fruit.fromString("Apple"));
			Assert.assertEquals(Fruit.BANANA, Fruit.fromString("Banana"));
			Assert.assertEquals(Fruit.ORANGE, Fruit.fromString("Orange"));
		}

		[Test]
		public function testGetAll():void {
			Assert.assertEquals(
				[ Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE ].toString(),
				Fruit.getAll().toString());
		}

		[Test(expects="Error")]
		public function anotherFruit():void {
			new Fruit("PLUM");
		}
	}
}

import common.Enumeration;

class Fruit extends Enumeration {

	private static const FRUIT:String = "Fruit";

	public static const APPLE:Fruit = new Fruit("Apple");
	public static const BANANA:Fruit = new Fruit("Banana");
	public static const ORANGE:Fruit = new Fruit("Orange");

	Enumeration.initialize(FRUIT);		

	public function Fruit(name:String) {
		super(FRUIT, name);
	}

	public static function getAll():Array {
		return Enumeration.getAll(FRUIT);
	}

	public static function fromString(name:String):Fruit {
		return Enumeration.fromString(FRUIT, name) as Fruit;
	}
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Will Del Genio</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-252081</link>
		<dc:creator>Will Del Genio</dc:creator>
		<pubDate>Thu, 07 Apr 2011 21:49:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-252081</guid>
		<description>I second Leandro&#039;s approach of using anonymous functions to assign values to static initializers.</description>
		<content:encoded><![CDATA[<p>I second Leandro's approach of using anonymous functions to assign values to static initializers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leandro Zanol</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-212890</link>
		<dc:creator>Leandro Zanol</dc:creator>
		<pubDate>Wed, 26 May 2010 18:00:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-212890</guid>
		<description>Hi Barney,

I&#039;ve struggled with static initializers for a while, the way you&#039;ve done it&#039;ll not allow you to run other code (such as &quot;for&quot;, &quot;if&quot;, etc.) than allocations.
The way I found to solve this was through anonymous function:

(function() {
...
})();

LZ</description>
		<content:encoded><![CDATA[<p>Hi Barney,</p>
<p>I've struggled with static initializers for a while, the way you've done it'll not allow you to run other code (such as "for", "if", etc.) than allocations.<br />
The way I found to solve this was through anonymous function:</p>
<p>(function() {<br />
&#8230;<br />
})();</p>
<p>LZ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-212884</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Wed, 26 May 2010 15:48:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-212884</guid>
		<description>jloa,

It&#039;s still not a &quot;real&quot; singleton, it&#039;s just a class with another type of magic to prevent uncontrolled instantiation.  Until AS3 supports private constructors, you can&#039;t have a real singleton, only &quot;close enough&quot; approximations through one of several approaches.</description>
		<content:encoded><![CDATA[<p>jloa,</p>
<p>It's still not a "real" singleton, it's just a class with another type of magic to prevent uncontrolled instantiation.  Until AS3 supports private constructors, you can't have a real singleton, only "close enough" approximations through one of several approaches.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jloa</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-212867</link>
		<dc:creator>jloa</dc:creator>
		<pubDate>Wed, 26 May 2010 07:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-212867</guid>
		<description>omg.... Ok, guys, here&#039;s what is called a real singleton class:

&lt;pre&gt;package
{
  public class Singleton
  {
    private static var INSTANCE:Singleton;

    public static function getInstance():Singleton
    {
       if(!INSTANCE) INSTANCE = new Singleton(new SingletonKey());
       return INSTANCE;
    }

    public function Singleton(key:SingletonKey = null)
    {
      if(!key) throw new Error(&quot;Singleton class&quot;);
    }
  }
}
internal class SingletonKey {}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>omg&#8230;. Ok, guys, here's what is called a real singleton class:</p>
<pre>package
{
  public class Singleton
  {
    private static var INSTANCE:Singleton;

    public static function getInstance():Singleton
    {
       if(!INSTANCE) INSTANCE = new Singleton(new SingletonKey());
       return INSTANCE;
    }

    public function Singleton(key:SingletonKey = null)
    {
      if(!key) throw new Error("Singleton class");
    }
  }
}
internal class SingletonKey {}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: BetaDesigns( Blog ).toString( ); &#187; Enums and static initializers in AS3</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-192181</link>
		<dc:creator>BetaDesigns( Blog ).toString( ); &#187; Enums and static initializers in AS3</dc:creator>
		<pubDate>Mon, 28 Sep 2009 21:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-192181</guid>
		<description>[...] post 1 [...]</description>
		<content:encoded><![CDATA[<p>[...] post 1 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Boyd</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-182709</link>
		<dc:creator>Jason Boyd</dc:creator>
		<pubDate>Sun, 21 Jun 2009 13:36:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-182709</guid>
		<description>Amendment to the singleton pattern above. A runtime error on attempting instantiation might suprise users who&#039;s tools or docs show a plain old default constructor. Likewise requiring a &quot;dummy&quot; parameter of a private class is clunky. Not that this isn&#039;t...

public class Foo {
  private static const MAGIC_NUM:Number = Math.random();
  private static const INSTANCE:Foo = new Foo(MAGIC_NUM); 

  public function Foo(_magicNum) {
    if (_magicNum != MAGIC_NUM)
       throw new Error(&quot;Singleton, dummy!&quot;); 
  }

  public function getInstance():Foo
  {
    return INSTANCE;
  }
}</description>
		<content:encoded><![CDATA[<p>Amendment to the singleton pattern above. A runtime error on attempting instantiation might suprise users who's tools or docs show a plain old default constructor. Likewise requiring a "dummy" parameter of a private class is clunky. Not that this isn't&#8230;</p>
<p>public class Foo {<br />
  private static const MAGIC_NUM:Number = Math.random();<br />
  private static const INSTANCE:Foo = new Foo(MAGIC_NUM); </p>
<p>  public function Foo(_magicNum) {<br />
    if (_magicNum != MAGIC_NUM)<br />
       throw new Error("Singleton, dummy!");<br />
  }</p>
<p>  public function getInstance():Foo<br />
  {<br />
    return INSTANCE;<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jordan</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-182499</link>
		<dc:creator>Jordan</dc:creator>
		<pubDate>Fri, 19 Jun 2009 16:09:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-182499</guid>
		<description>I think that I have found a better way to do it, you don&#039;t need the locked variable
and with the &quot;private&quot; class in the constructor it can&#039;t be instantiated ,
unless you pass null to the constructor , then runtime Error will occur :)!

&lt;pre&gt;
package {
	final public class ColorEnum  {
		//STATIC
		public static const BLACK:ColorEnum = new ColorEnum(0x000000,null);
		public static const WHITE:ColorEnum = new ColorEnum(0xFFFFFF,null);

		//INSTANCE
		private var _color:uint

		public function get color():uint  {
			return _color
		}
		
		function ColorEnum(color:uint,block_:block_constructor)  {
			if (ColorEnum) throw new Error(&quot;You can&#039;t instantiate ColorEnum&quot;);
			_color = color;
		}
	}
}

class block_constructor {}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I think that I have found a better way to do it, you don't need the locked variable<br />
and with the "private" class in the constructor it can't be instantiated ,<br />
unless you pass null to the constructor , then runtime Error will occur :)!</p>
<pre>
package {
	final public class ColorEnum  {
		//STATIC
		public static const BLACK:ColorEnum = new ColorEnum(0x000000,null);
		public static const WHITE:ColorEnum = new ColorEnum(0xFFFFFF,null);

		//INSTANCE
		private var _color:uint

		public function get color():uint  {
			return _color
		}

		function ColorEnum(color:uint,block_:block_constructor)  {
			if (ColorEnum) throw new Error("You can't instantiate ColorEnum");
			_color = color;
		}
	}
}

class block_constructor {}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Enumerations with Class &#171; Flex Insights</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-176097</link>
		<dc:creator>Enumerations with Class &#171; Flex Insights</dc:creator>
		<pubDate>Sun, 03 May 2009 01:46:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-176097</guid>
		<description>[...] http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/ [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/" rel="nofollow">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avangel</title>
		<link>https://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-153619</link>
		<dc:creator>Avangel</dc:creator>
		<pubDate>Thu, 08 Jan 2009 11:03:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-153619</guid>
		<description>Many thanks for this code trick :) I was trying to find solutions to emulate enums in AS3 for a while, without success. Your solutions is pretty good. It&#039;s a shame that AS does not enable protected/private contructors, it would have been even easier...</description>
		<content:encoded><![CDATA[<p>Many thanks for this code trick :) I was trying to find solutions to emulate enums in AS3 for a while, without success. Your solutions is pretty good. It's a shame that AS does not enable protected/private contructors, it would have been even easier&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
