<?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>http://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, 18 Mar 2010 15:02:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: BetaDesigns( Blog ).toString( ); &#187; Enums and static initializers in AS3</title>
		<link>http://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>http://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>http://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>http://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>http://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>
	<item>
		<title>By: ActionScript 3 Enums &#171; The Combined Corner</title>
		<link>http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-138320</link>
		<dc:creator>ActionScript 3 Enums &#171; The Combined Corner</dc:creator>
		<pubDate>Sun, 02 Nov 2008 21:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-138320</guid>
		<description>[...] different ways of faking Enums in ActionScript 3, I decided to do my own based on Scott&#8217;s and Barney&#8217;s [...]</description>
		<content:encoded><![CDATA[<p>[...] different ways of faking Enums in ActionScript 3, I decided to do my own based on Scott's and Barney's [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ActionScript Enums &#171; The Combined Corner</title>
		<link>http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-136817</link>
		<dc:creator>ActionScript Enums &#171; The Combined Corner</dc:creator>
		<pubDate>Sun, 26 Oct 2008 19:30:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-136817</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: Jason Boyd</title>
		<link>http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-130902</link>
		<dc:creator>Jason Boyd</dc:creator>
		<pubDate>Mon, 06 Oct 2008 19:29:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-130902</guid>
		<description>True and true. I tend to code all my AS3 assuming single threads, since AS3 is single-threaded. If they change this in AS4 or whenever, all bets are off.</description>
		<content:encoded><![CDATA[<p>True and true. I tend to code all my AS3 assuming single threads, since AS3 is single-threaded. If they change this in AS4 or whenever, all bets are off.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: barneyb</title>
		<link>http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-130881</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Mon, 06 Oct 2008 17:30:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-130881</guid>
		<description>Jason,

The static initializer has a subtle benefit: no race condition.  It&#039;s theoretically possible with the constructor-conditional method that two threads could instantiate objects at the same time and you end up with to singletons.  Currently, the Adobe Flash player is single threaded so it&#039;s not actually possible, but that&#039;s an implementation detail.

Because the static initializer happens at class load time, and you&#039;ll never see the same class loaded more than once, you&#039;re safe.  A subtle point, and with the current implementation an irrelevant one, but still worth mentioning, I think.</description>
		<content:encoded><![CDATA[<p>Jason,</p>
<p>The static initializer has a subtle benefit: no race condition.  It's theoretically possible with the constructor-conditional method that two threads could instantiate objects at the same time and you end up with to singletons.  Currently, the Adobe Flash player is single threaded so it's not actually possible, but that's an implementation detail.</p>
<p>Because the static initializer happens at class load time, and you'll never see the same class loaded more than once, you're safe.  A subtle point, and with the current implementation an irrelevant one, but still worth mentioning, I think.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason Boyd</title>
		<link>http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/comment-page-1/#comment-130880</link>
		<dc:creator>Jason Boyd</dc:creator>
		<pubDate>Mon, 06 Oct 2008 17:24:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/#comment-130880</guid>
		<description>Having seen several approaches to the Singleton case in AS3 (non-public class in constructor, using a random and/or private number, etc), this one works pretty well with minimal fuss: 

package foo
{
  class Singleton
  {
    private static const m_instance:Singleton = new Singleton();

    public function Singleton()
    {
      if (m_instance != null) throw new Error(&quot;Noooooooo!!!!&quot;);
      // one time init code here
    }

    public static function getInstance():Singleton
    {
      return m_instance;
    }
  }
}

Static initializers are cool though. Thanks for the post!</description>
		<content:encoded><![CDATA[<p>Having seen several approaches to the Singleton case in AS3 (non-public class in constructor, using a random and/or private number, etc), this one works pretty well with minimal fuss: </p>
<p>package foo<br />
{<br />
  class Singleton<br />
  {<br />
    private static const m_instance:Singleton = new Singleton();</p>
<p>    public function Singleton()<br />
    {<br />
      if (m_instance != null) throw new Error("Noooooooo!!!!");<br />
      // one time init code here<br />
    }</p>
<p>    public static function getInstance():Singleton<br />
    {<br />
      return m_instance;<br />
    }<br />
  }<br />
}</p>
<p>Static initializers are cool though. Thanks for the post!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
