<?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: Typesafe Enum Pattern in AS3</title>
	<atom:link href="http://www.herrodius.com/blog/87/feed" rel="self" type="application/rss+xml" />
	<link>http://www.herrodius.com/blog/87</link>
	<description>Thoughts from a software developer</description>
	<lastBuildDate>Fri, 12 Mar 2010 14:56:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: DamionMurray</title>
		<link>http://www.herrodius.com/blog/87/comment-page-1#comment-13829</link>
		<dc:creator>DamionMurray</dc:creator>
		<pubDate>Mon, 23 Feb 2009 03:26:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.herrodius.com/blog/?p=87#comment-13829</guid>
		<description>@JoshMcDonald: Once the class is declared as final this implementation of
enumerated types does prove quite useful. The reason the author prevents
further instantiations of the class is to ensure strict adherence to the very
definition of an enumerated type; a type with a fixed number of predefined values.
This &quot;strictness&quot; may seem unnecessary but consider the following scenario. A
method takes a DebitCreditEnum instance as a parameter. Inside the method body the
enum parameter is tested for equality with static constants of the enum class.
 Now, if instantiation of an enum were possible, the method could be passed a DebitCreditEnum instance independent of those declared as static constants. 
In that case, the test in the method body would fail, but this error would crop
up at run-time and be more difficult to debug compared to a compile-time error.</description>
		<content:encoded><![CDATA[<p>@JoshMcDonald: Once the class is declared as final this implementation of<br />
enumerated types does prove quite useful. The reason the author prevents<br />
further instantiations of the class is to ensure strict adherence to the very<br />
definition of an enumerated type; a type with a fixed number of predefined values.<br />
This &#8220;strictness&#8221; may seem unnecessary but consider the following scenario. A<br />
method takes a DebitCreditEnum instance as a parameter. Inside the method body the<br />
enum parameter is tested for equality with static constants of the enum class.<br />
 Now, if instantiation of an enum were possible, the method could be passed a DebitCreditEnum instance independent of those declared as static constants.<br />
In that case, the test in the method body would fail, but this error would crop<br />
up at run-time and be more difficult to debug compared to a compile-time error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.herrodius.com/blog/87/comment-page-1#comment-13721</link>
		<dc:creator>David</dc:creator>
		<pubDate>Fri, 07 Nov 2008 19:30:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.herrodius.com/blog/?p=87#comment-13721</guid>
		<description>I recently came across the problem of neededing some enumerations in code I was writing.  I tried to come up with an enumerated type as close to the ones used in c# as I could.  I saw that an enum type was simply a collection of string values each basically creates as a static member of the enum type.  I came up with a base enum class that then can be exteneded into any enum you wish.

package Enums.baseEnum
{
	
	/** the base class for an enumerated type.  The values for the type are determined by a string array that is passed in.  A user can get/set the current
	 * value and the current index.  Passing an invalid index or and invalid value to the setters will result in an argument error.  To create a more standard
	 * enum type, simply extend this class, creating as many public static constants as you wish, then make an array of all the same constants, and pass to
	 * the super constructor of this class.  For ease of use, remembering intellisence will alphabetize your enum lists, so keep your strings alphabetized
	 * or include the index as part of the static members name**/
	public class Enum
	{		
		public function Enum(values:Array, initialValue:String)
		{
			if (values == null)
				_values = new Array();
			else
				_values = values;
			this.value = defaultValue;
		}

		protected var _values:Array;
		
		private var _index:int;
				
		public function get index():int
		{
			return _index;
		}
		public function set index(val:int):void
		{
			if(val = _values.length)
				throw new ArgumentError(&quot;Enum set index : supplied value of &quot; + val + &quot; is outside current range of 0 - &quot; + _values.length - 1 + &quot;, determined by this Enum&#039;s array [&quot; + _values.toString()&quot;]&quot;);
			_index = value;
		}
		
		public function set value(value:String):void
		{
			_index = _values.indexOf(value);
			if(_index == -1)
				throw new ArgumentError(&quot;Enum set value : supplied value of &quot; + value + &quot; is not a recognized entry, available values are &quot; + _values.toString());
		}
		public function get value():String
		{
			return _values[_index].toString();
		}
	}
}

this class requires an array passed to it.  In common use, i believe an array of strings should suffice, however, I am sure objects could also be passed if needed.  The next step is to extend the Enum class with whatever enumerated type you would like, creating static strings for each possible value, and then passing that array in the constructor ... as follows

package Enums
{
	import Enums.baseEnum.Enum;

	public class Colors extends Enum
	{
		public static const Black:String = &#039;Black&#039;;
		public static const Blue:String = &#039;Blue&#039;;
		public static const Green:String = &#039;Green&#039;;
		public static const Red:String = &#039;Red&#039;;
		
		public function EquipmentType(initialValue:String)
		{
			super([&#039;Black&#039;,&#039;Blue&#039;,&#039;Green&#039;,&#039;Red&#039;],initialValue);
		}
	}
}

then in use of this program, one can create as many Colors objects they want, and each one may be set using the set value call and the ClassName. Static variable.... i.e.

private var myColor:Colors = new Colors(Colors.Blue);

use myColor.value to get the set value or myColor.value = Colors.XXXXX to set the value.

What does everyone think?</description>
		<content:encoded><![CDATA[<p>I recently came across the problem of neededing some enumerations in code I was writing.  I tried to come up with an enumerated type as close to the ones used in c# as I could.  I saw that an enum type was simply a collection of string values each basically creates as a static member of the enum type.  I came up with a base enum class that then can be exteneded into any enum you wish.</p>
<p>package Enums.baseEnum<br />
{</p>
<p>	/** the base class for an enumerated type.  The values for the type are determined by a string array that is passed in.  A user can get/set the current<br />
	 * value and the current index.  Passing an invalid index or and invalid value to the setters will result in an argument error.  To create a more standard<br />
	 * enum type, simply extend this class, creating as many public static constants as you wish, then make an array of all the same constants, and pass to<br />
	 * the super constructor of this class.  For ease of use, remembering intellisence will alphabetize your enum lists, so keep your strings alphabetized<br />
	 * or include the index as part of the static members name**/<br />
	public class Enum<br />
	{<br />
		public function Enum(values:Array, initialValue:String)<br />
		{<br />
			if (values == null)<br />
				_values = new Array();<br />
			else<br />
				_values = values;<br />
			this.value = defaultValue;<br />
		}</p>
<p>		protected var _values:Array;</p>
<p>		private var _index:int;</p>
<p>		public function get index():int<br />
		{<br />
			return _index;<br />
		}<br />
		public function set index(val:int):void<br />
		{<br />
			if(val = _values.length)<br />
				throw new ArgumentError(&#8220;Enum set index : supplied value of &#8221; + val + &#8221; is outside current range of 0 &#8211; &#8221; + _values.length &#8211; 1 + &#8220;, determined by this Enum&#8217;s array [" + _values.toString()"]&#8220;);<br />
			_index = value;<br />
		}</p>
<p>		public function set value(value:String):void<br />
		{<br />
			_index = _values.indexOf(value);<br />
			if(_index == -1)<br />
				throw new ArgumentError(&#8220;Enum set value : supplied value of &#8221; + value + &#8221; is not a recognized entry, available values are &#8221; + _values.toString());<br />
		}<br />
		public function get value():String<br />
		{<br />
			return _values[_index].toString();<br />
		}<br />
	}<br />
}</p>
<p>this class requires an array passed to it.  In common use, i believe an array of strings should suffice, however, I am sure objects could also be passed if needed.  The next step is to extend the Enum class with whatever enumerated type you would like, creating static strings for each possible value, and then passing that array in the constructor &#8230; as follows</p>
<p>package Enums<br />
{<br />
	import Enums.baseEnum.Enum;</p>
<p>	public class Colors extends Enum<br />
	{<br />
		public static const Black:String = &#8216;Black&#8217;;<br />
		public static const Blue:String = &#8216;Blue&#8217;;<br />
		public static const Green:String = &#8216;Green&#8217;;<br />
		public static const Red:String = &#8216;Red&#8217;;</p>
<p>		public function EquipmentType(initialValue:String)<br />
		{<br />
			super(['Black','Blue','Green','Red'],initialValue);<br />
		}<br />
	}<br />
}</p>
<p>then in use of this program, one can create as many Colors objects they want, and each one may be set using the set value call and the ClassName. Static variable&#8230;. i.e.</p>
<p>private var myColor:Colors = new Colors(Colors.Blue);</p>
<p>use myColor.value to get the set value or myColor.value = Colors.XXXXX to set the value.</p>
<p>What does everyone think?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh McDonald</title>
		<link>http://www.herrodius.com/blog/87/comment-page-1#comment-13655</link>
		<dc:creator>Josh McDonald</dc:creator>
		<pubDate>Sun, 14 Sep 2008 23:24:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.herrodius.com/blog/?p=87#comment-13655</guid>
		<description>An interesting technique, but it does rely on the compiler not one day being optimised in a way that the static code blocks happen before static initialisations. The other question is of course since you&#039;re going to be using typesafe checks against the public consts (and DebitCreditEnum is not final), what benefit do you really get by this code? Who cares if somebody calls new DebitCreditEnum() a few times? It can&#039;t break anything relying on this enum class as far as I can see.</description>
		<content:encoded><![CDATA[<p>An interesting technique, but it does rely on the compiler not one day being optimised in a way that the static code blocks happen before static initialisations. The other question is of course since you&#8217;re going to be using typesafe checks against the public consts (and DebitCreditEnum is not final), what benefit do you really get by this code? Who cares if somebody calls new DebitCreditEnum() a few times? It can&#8217;t break anything relying on this enum class as far as I can see.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ciprian</title>
		<link>http://www.herrodius.com/blog/87/comment-page-1#comment-13629</link>
		<dc:creator>Ciprian</dc:creator>
		<pubDate>Thu, 14 Aug 2008 08:40:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.herrodius.com/blog/?p=87#comment-13629</guid>
		<description>I had a similar problem but I found a work-around. It&#039;s not the nicest one, but you could instantiate your DEBIT and CREDIT in the constructor of the DebitCreditEnum class. You put them as vars, you only provide getters (to make them constant to the outside), you set them as null in the beginning, and in the constructor you test if they are null in wich case you instantiate them.</description>
		<content:encoded><![CDATA[<p>I had a similar problem but I found a work-around. It&#8217;s not the nicest one, but you could instantiate your DEBIT and CREDIT in the constructor of the DebitCreditEnum class. You put them as vars, you only provide getters (to make them constant to the outside), you set them as null in the beginning, and in the constructor you test if they are null in wich case you instantiate them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chris</title>
		<link>http://www.herrodius.com/blog/87/comment-page-1#comment-12886</link>
		<dc:creator>chris</dc:creator>
		<pubDate>Fri, 11 Jan 2008 15:39:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.herrodius.com/blog/?p=87#comment-12886</guid>
		<description>Thanks for the tip!

I too was trying the initial approach, which led me to your post.</description>
		<content:encoded><![CDATA[<p>Thanks for the tip!</p>
<p>I too was trying the initial approach, which led me to your post.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
