<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>devboy</title>
	<atom:link href="http://www.devboy.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devboy.org</link>
	<description>Web, App and Software Developer with passion!</description>
	<lastBuildDate>Wed, 30 Nov 2011 11:21:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>learn haXe: structural typing</title>
		<link>http://www.devboy.org/2011/11/29/learn-haxe-structure-types/</link>
		<comments>http://www.devboy.org/2011/11/29/learn-haxe-structure-types/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 21:12:16 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Learn haXe - the hard way]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=722</guid>
		<description><![CDATA[Welcome to the third post of my Learn haXe &#8211; the hard way series. In cased you missed one of the previous posts you can find them here. This time we are going to talk about a little gem in the haXe language called structure types and are incredibly useful. Structure types If you have read my post [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the third post of my <strong>Learn haXe &#8211; the hard way</strong> series. In cased you missed one of the previous posts <a href="http://www.devboy.org/category/blog/learn-haxe/">you can find them here</a>. This time we are going to talk about a little gem in the haXe language called <strong>structure types</strong> and are incredibly useful.</p>
<h2>Structure types</h2>
<p>If you have read my post on <a title="Understanding function types in haXe" href="http://www.devboy.org/2011/11/22/understanding-function-types-in-haxe/">function types in haxe</a> you will know that the type-system in haXe is different to other languages like ActionScript 3 or Java. In haXe types are not bound to classes or interfaces, they are their own different &#8220;thing&#8221; and therefore allow more flexible typing scenarios.</p>
<p>One feature this type-systems allows are the so called structure types which can be used to type a variable to a subset of properties that the actual type contains. The simplest illustration is probably to define a variable with an anonymous object and let haXe figure out what type the variable has through type-inference.</p>
<div id="gist-1406722" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">circle</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="mi">100</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">100</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="mi">50</span> <span class="o">};</span></div><div class='line' id='LC2'><span class="n">type</span><span class="o">(</span><span class="n">circle</span><span class="o">);</span> <span class="c1">// returns { y : Int, x : Int, radius : Int }</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1406722/c1b1d7802f55ec84d003f6c6f3de44b2370381fd/AnonymousType.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1406722#file_anonymous_type.as" style="float:right;margin-right:10px;color:#666">AnonymousType.as</a>
            <a href="https://gist.github.com/1406722">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>HaXe tells us that the type of &#8220;circle&#8221; is a structure type with three properties: x, y and radius which are all inferred to the type Int. So our variable became type-safe all of a sudden, which means we are not able to change the type of &#8220;circle&#8221; or one of it&#8217;s properties from now on:</p>
<div id="gist-1406783" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">circle</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="mi">100</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">100</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="mi">50</span> <span class="o">}</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">circle</span><span class="o">.</span><span class="na">x</span> <span class="o">=</span> <span class="mi">90</span><span class="o">;</span> <span class="c1">// will work fine</span></div><div class='line' id='LC4'><span class="n">circle</span><span class="o">.</span><span class="na">y</span> <span class="o">=</span> <span class="s2">&quot;100&quot;</span><span class="o">;</span> <span class="c1">// error: String should be Int</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="n">circle</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="mi">90</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">80</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="mi">10</span> <span class="o">}</span> <span class="c1">// will work fine</span></div><div class='line' id='LC7'><span class="n">circle</span> <span class="o">=</span> <span class="s2">&quot;circle&quot;</span><span class="o">;</span> <span class="c1">// error: String should be { y : Int, x : Int, radius : Int }</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1406783/1edd357c182f97c8e5cca5ab0a35c76a34cbb4e2/Structure.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1406783#file_structure.as" style="float:right;margin-right:10px;color:#666">Structure.as</a>
            <a href="https://gist.github.com/1406783">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>To be clear: This is a good thing! If you don&#8217;t want automatic type-inference, you can always use Dynamic as type. Now that we know a little bit about the structure type we can also use them to type variables manually:</p>
<div id="gist-1406867" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">circle</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="n">Int</span> <span class="o">};</span></div><div class='line' id='LC2'><span class="n">circle</span> <span class="o">=</span> <span class="o">{</span><span class="n">x</span><span class="o">:</span> <span class="mi">10</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">10</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="mi">10</span><span class="o">};</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1406867/5921e2b08fd6b4f9b86051d1a9a3b99426e837dd/Type.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1406867#file_type.as" style="float:right;margin-right:10px;color:#666">Type.as</a>
            <a href="https://gist.github.com/1406867">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Pretty simple so far, let&#8217;s investigate another useful case of structure types: polymorphism. In the beginning of this post we said that a structure type defines a subset of the actual properties of an object, which simply means that it doesn&#8217;t care about all the other properties. This makes it really easy to work with lots of different types that share a common subset, such as position coordinates x and y:</p>
<div id="gist-1408583" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">circle</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="n">Int</span> <span class="o">}</span>  <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="mi">10</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">10</span><span class="o">,</span> <span class="n">radius</span><span class="o">:</span> <span class="mi">10</span> <span class="o">};</span></div><div class='line' id='LC2'><span class="n">var</span> <span class="n">rect</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">width</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">height</span><span class="o">:</span> <span class="n">Int</span> <span class="o">}</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="mi">20</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="mi">20</span><span class="o">,</span> <span class="n">width</span><span class="o">:</span> <span class="mi">100</span><span class="o">,</span> <span class="n">height</span><span class="o">:</span> <span class="mi">100</span> <span class="o">};</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="kd">function </span><span class="nf">move</span><span class="o">(</span> <span class="err">objectWithXAndY:</span> <span class="err">{</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Int</span> <span class="err">}</span> <span class="o">)</span></div><div class='line' id='LC5'><span class="o">{</span></div><div class='line' id='LC6'>	<span class="n">objectWithXAndY</span><span class="o">.</span><span class="na">x</span> <span class="o">+=</span> <span class="mi">5</span><span class="o">;</span></div><div class='line' id='LC7'>	<span class="n">objectWithXAndY</span><span class="o">.</span><span class="na">y</span> <span class="o">+=</span> <span class="mi">5</span><span class="o">;</span></div><div class='line' id='LC8'><span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="n">move</span><span class="o">(</span> <span class="n">circle</span> <span class="o">);</span></div><div class='line' id='LC11'><span class="n">move</span><span class="o">(</span> <span class="n">rect</span> <span class="o">);</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1408583/7f4372ca717f2c46bf28cd529ae52b4bf31cb522/strctuMethod.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1408583#file_strctu_method.as" style="float:right;margin-right:10px;color:#666">strctuMethod.as</a>
            <a href="https://gist.github.com/1408583">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Of course you can apply this to objects that are instances of a class, like Point, as well:</p>
<div id="gist-1407024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">point</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Float</span> <span class="o">}</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">Point</span><span class="o">(</span><span class="mi">10</span><span class="o">,</span><span class="mi">10</span><span class="o">);</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1407024/80f0e2012a2234bd1c9baddaac98bb6dc6a91898/point.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1407024#file_point.as" style="float:right;margin-right:10px;color:#666">point.as</a>
            <a href="https://gist.github.com/1407024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>One thing that you will have to keep in mind is that we need to type our variables &#8220;circle&#8221; and &#8220;rect&#8221; in the above example, as type-inference would type them to a constant type and that would not allow you to assign to another type with a subset of its properties. A little cheat-sheet explaining what works and what doesn&#8217;t:</p>
<div id="gist-1408696" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">point</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Int</span> <span class="o">};</span></div><div class='line' id='LC2'><span class="n">var</span>	<span class="n">pointA3D</span><span class="o">:</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">z</span><span class="o">:</span> <span class="n">Int</span> <span class="o">}</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">z</span><span class="o">:</span><span class="mi">10</span> <span class="o">};</span> </div><div class='line' id='LC3'><span class="n">var</span>	<span class="n">pointB3D</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">z</span><span class="o">:</span><span class="mi">10</span> <span class="o">};</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">point</span> <span class="o">=</span> <span class="n">pointA3D</span><span class="o">;</span> <span class="c1">// this works fine, as the type is set</span></div><div class='line' id='LC6'><span class="n">point</span> <span class="o">=</span> <span class="n">pointB3D</span><span class="o">;</span> <span class="c1">// error: { z : Int, y : Int, x : Int } has extra field z</span></div><div class='line' id='LC7'><span class="n">point</span> <span class="o">=</span> <span class="o">{</span> <span class="n">x</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span><span class="mi">10</span><span class="o">,</span> <span class="n">z</span><span class="o">:</span><span class="mi">10</span> <span class="o">};</span> <span class="c1">// { z : Int, y : Int, x : Int } has extra field z</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1408696/6f141b738834a3643003d9565bfd5e2f169eb294/Type.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1408696#file_type.as" style="float:right;margin-right:10px;color:#666">Type.as</a>
            <a href="https://gist.github.com/1408696">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/29/learn-haxe-structure-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>learn haXe: extension methods</title>
		<link>http://www.devboy.org/2011/11/26/learn-haxe-extension-methods/</link>
		<comments>http://www.devboy.org/2011/11/26/learn-haxe-extension-methods/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 13:31:30 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Learn haXe - the hard way]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=699</guid>
		<description><![CDATA[In the second post of my series Learn haXe &#8211; the hard way we will have a look at &#8220;extension methods&#8221; and why they are the best thing since sliced bread. Check also my first post on &#8220;partial function application&#8221; which is a similar concept and might actually be used behind the scenes to enable &#8220;extension [...]]]></description>
			<content:encoded><![CDATA[<p>In the second post of my series <strong>Learn haXe &#8211; the hard way</strong> we will have a look at &#8220;extension methods&#8221; and why they are the best thing since sliced bread. Check also my <a title="learn haXe: partial function application" href="http://www.devboy.org/2011/11/25/partial-function-application/">first post on &#8220;partial function application&#8221;</a> which is a similar concept and might actually be used behind the scenes to enable &#8220;extension methods&#8221;.</p>
<h2>The &#8220;old&#8221; way</h2>
<p>If you&#8217;re coming from ActionScript 3, or another statically typed language, you probably write a lot of static helper methods to manipulate native objects like String or Array. We do this because adding functionality to these types requires monkey-patching, like prototype in AS3, which is not always the preferred solution. For example when you want to delete or keep elements of an array based on a condition you would create a class like the following:</p>
<div id="gist-1395537" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">package</span> <span class="nn">org.devboy</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">class</span> <span class="n">ArrayUtils</span> </div><div class='line' id='LC4'><span class="o">{</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kd">public</span> <span class="kd">static</span> <span class="kd">function</span> <span class="n">delete_if</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;(</span> <span class="n">array</span><span class="o">:</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;,</span> <span class="n">processor</span><span class="o">:</span> <span class="n">T</span> <span class="o">-&gt;</span> <span class="n">Bool</span> <span class="o">):</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span></div><div class='line' id='LC7'>	<span class="o">{</span></div><div class='line' id='LC8'>		<span class="kd">var</span> <span class="n">remove</span><span class="p">:</span> <span class="kt">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">=</span> <span class="o">[];</span></div><div class='line' id='LC9'>		<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">array</span> <span class="o">)</span></div><div class='line' id='LC10'>			<span class="k">if</span> <span class="o">(</span> <span class="n">processor</span><span class="o">(</span><span class="n">item</span><span class="o">)</span> <span class="o">)</span></div><div class='line' id='LC11'>				<span class="n">remove</span><span class="o">.</span><span class="na">push</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC12'>		<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">remove</span> <span class="o">)</span></div><div class='line' id='LC13'>			<span class="n">array</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC14'>		<span class="k">return</span> <span class="n">array</span><span class="o">;</span></div><div class='line' id='LC15'>	<span class="o">}</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>	<span class="kd">public</span> <span class="kd">static</span> <span class="kd">function</span> <span class="n">keep_if</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;(</span> <span class="n">array</span><span class="o">:</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;,</span> <span class="n">processor</span><span class="o">:</span> <span class="n">T</span> <span class="o">-&gt;</span> <span class="n">Bool</span> <span class="o">):</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span></div><div class='line' id='LC18'>	<span class="o">{</span></div><div class='line' id='LC19'>		<span class="kd">var</span> <span class="n">remove</span><span class="p">:</span> <span class="kt">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">=</span> <span class="o">[];</span></div><div class='line' id='LC20'>		<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">array</span> <span class="o">)</span></div><div class='line' id='LC21'>			<span class="k">if</span> <span class="o">(</span> <span class="o">!</span><span class="n">processor</span><span class="o">(</span><span class="n">item</span><span class="o">)</span> <span class="o">)</span></div><div class='line' id='LC22'>				<span class="n">remove</span><span class="o">.</span><span class="na">push</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC23'>		<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">remove</span> <span class="o">)</span></div><div class='line' id='LC24'>			<span class="n">array</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC25'>		<span class="k">return</span> <span class="n">array</span><span class="o">;</span></div><div class='line' id='LC26'>	<span class="o">}</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1395537/779058212a6bf819375bb01c80ffa0864ccde4fb/ArrayUtils.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1395537#file_array_utils.as" style="float:right;margin-right:10px;color:#666">ArrayUtils.as</a>
            <a href="https://gist.github.com/1395537">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You would then use the static methods of ArrayUtils to manipulate your arrays:</p>
<div id="gist-1395551" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">import</span> <span class="nn">org.devboy.ArrayUtils</span><span class="o">;</span>		</div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">var</span> <span class="n">is_c</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">==</span> <span class="s2">&quot;c&quot;</span><span class="o">;</span> <span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">ArrayUtils</span><span class="o">.</span><span class="na">delete_if</span><span class="o">(</span> <span class="o">[</span> <span class="s2">&quot;a&quot;</span><span class="o">,</span> <span class="s2">&quot;b&quot;</span><span class="o">,</span> <span class="s2">&quot;c&quot;</span> <span class="o">],</span> <span class="n">is_c</span> <span class="o">);</span> <span class="c1">// returns [ &quot;a&quot;, &quot;b&quot; ]</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1395551/382a9c4a72c352b92bf12faf385bce76956fb81a/useArrayUtilsStatic.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1395551#file_use_array_utils_static.as" style="float:right;margin-right:10px;color:#666">useArrayUtilsStatic.as</a>
            <a href="https://gist.github.com/1395551">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Nothing wrong with that but let me show you a smarter and cleaner solution.</p>
<h2>Extension methods</h2>
<p>Extension methods are static methods, so no reason to change our static methods in the ArrayUtils class. The difference is that we can call extension methods as if they were instance methods which will result in much cleaner and more readable code. Let&#8217;s use our ArrayUtils example from above, but this time as extension method:</p>
<div id="gist-1395626" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">using</span> <span class="n">org</span><span class="o">.</span><span class="na">devboy</span><span class="o">.</span><span class="na">ArrayUtils</span><span class="o">;</span>		</div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">var</span> <span class="n">is_c</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">==</span> <span class="s2">&quot;c&quot;</span><span class="o">;</span> <span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="o">[</span> <span class="s2">&quot;a&quot;</span><span class="o">,</span> <span class="s2">&quot;b&quot;</span><span class="o">,</span> <span class="s2">&quot;c&quot;</span> <span class="o">].</span><span class="n">delete_if</span><span class="o">(</span> <span class="n">is_c</span> <span class="o">);</span> <span class="c1">// returns [ &quot;a&quot;, &quot;b&quot; ]</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1395626/c36e1173996ceed04abc799dd7e090d6de7748c7/useArrayUtilsExtension.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1395626#file_use_array_utils_extension.as" style="float:right;margin-right:10px;color:#666">useArrayUtilsExtension.as</a>
            <a href="https://gist.github.com/1395626">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Instead of import we used the &#8220;using&#8221; keyword to instruct the haXe compiler to make the static methods of ArrayUtils available as extension methods. Now we are able to call the &#8220;delete_if&#8221; method as if it was part of the Array class, but how does this work? Let&#8217;s compare the types of the static and instance method:</p>
<div id="gist-1395659" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">import</span> <span class="nn">org.devboy.ArrayUtils</span><span class="o">;</span></div><div class='line' id='LC2'><span class="n">using</span> <span class="n">org</span><span class="o">.</span><span class="na">devboy</span><span class="o">.</span><span class="na">ArrayUtils</span><span class="o">;</span></div><div class='line' id='LC3'><span class="n">type</span><span class="o">(</span> <span class="n">ArrayUtils</span><span class="o">.</span><span class="na">delete_if</span> <span class="o">);</span> <span class="c1">// Array&lt;T&gt; -&gt; (T -&gt; Bool) -&gt; Array&lt;T&gt; </span></div><div class='line' id='LC4'><span class="n">type</span><span class="o">(</span> <span class="o">[].</span><span class="n">delete_if</span> <span class="o">);</span> <span class="c1">// (T -&gt; Bool) -&gt; Array&lt;T&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1395659/ebad06cbd48746f8708f193993308542a3c0d85b/typeOfExtensionMethods.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1395659#file_type_of_extension_methods.as" style="float:right;margin-right:10px;color:#666">typeOfExtensionMethods.as</a>
            <a href="https://gist.github.com/1395659">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As you can see, the instance method is a subset of the static method because the first parameter has been <a title="learn haXe: partial function application" href="http://www.devboy.org/2011/11/25/partial-function-application/">partially applied</a> to the instance it is called on. Therefore we can conclude that haXe  will provide you with an extension method when the type of your instance variable matches the type of the first parameter of your static method.</p>
<h2>Use cases</h2>
<p>It&#8217;s pretty obvious that using extension methods will allow you to build much cleaner APIs for you libraries and helper classes, just as if they were part of the original implementations.</p>
<p>You can also create ruby-like DSLs with extension methods like:</p>
<div id="gist-1395751" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="mi">5</span><span class="o">.</span><span class="na">times</span><span class="o">(</span> <span class="n">callback</span><span class="o">(</span> <span class="nf">trace</span><span class="o">,</span> <span class="s2">&quot;Hello&quot;</span> <span class="o">)</span> <span class="o">)</span> <span class="c1">//traces Hello 5 times</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1395751/6443a64ba5018278d12fabcb5be7333a29d57e93/5Times.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1395751#file_5_times.as" style="float:right;margin-right:10px;color:#666">5Times.as</a>
            <a href="https://gist.github.com/1395751">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/26/learn-haxe-extension-methods/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>learn haXe: partial function application</title>
		<link>http://www.devboy.org/2011/11/25/partial-function-application/</link>
		<comments>http://www.devboy.org/2011/11/25/partial-function-application/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 22:21:58 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Learn haXe - the hard way]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=646</guid>
		<description><![CDATA[This is the first post in my series: Learn haXe &#8211; the hard way where I will shed some light on various topics around haXe. It&#8217;s called &#8220;the hard way&#8221; because that is the route I am taking myself to learn haXe: Diving extensively into every little feature, disassembling and reassembling it until I fully understand how [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post in my series:<strong> Learn haXe &#8211; the hard way</strong> where I will shed some light on various topics around haXe. It&#8217;s called &#8220;the hard way&#8221; because that is the route I am taking myself to learn haXe: Diving extensively into every little feature, disassembling and reassembling it until I fully understand how it works and what its use cases are.</p>
<p>Let&#8217;s just dive into the first topic: partial function application! It might be useful to read up some background information on <a title="Understanding function types in haXe" href="http://www.devboy.org/2011/11/22/understanding-function-types-in-haxe/">functions in haXe in a previous post</a>.</p>
<h2>Partial function application</h2>
<p>In case you have never heard of partial (function) application, let&#8217;s have a look at what <a href="http://en.wikipedia.org/wiki/Partial_application" target="_blank">Wikipedia</a> has to say about it:</p>
<blockquote><p>In computer science, <strong>partial application</strong> (or <strong>partial function application</strong>) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function , we might fix (or &#8216;bind&#8217;) the first argument, producing a function of type . Evaluation of this function might be represented as <em>f</em><em>partial</em>(2,3). Note that the result of partial function application in this case is a function that takes two arguments.</p></blockquote>
<p>This simply states that a partial function application is a subset of a regular function which has been partially applied, and I can guarantee that you are already using partial application, in some form, in your code today. Does the following look familiar?</p>
<div id="gist-1394366" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">add</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="kd">function </span><span class="nf">addOne</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC6'>	<span class="k">return</span> <span class="n">add</span><span class="o">(</span> <span class="mi">1</span><span class="o">,</span> <span class="n">x</span> <span class="o">);</span></div><div class='line' id='LC7'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1394366/60b8559420bda85cf0966779785526fa5c511fec/manualPartialApplication.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1394366#file_manual_partial_application.as" style="float:right;margin-right:10px;color:#666">manualPartialApplication.as</a>
            <a href="https://gist.github.com/1394366">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The function &#8220;addOne&#8221; is actually a partial application of the function &#8220;add&#8221;.  It takes a parameter less than &#8220;add&#8221; and then applies 1 and the given parameter to &#8220;add&#8221;. Therefore &#8220;add&#8221; has been partially applied with 1.</p>
<p>In haXe there is no need to write these kind of functions by hand, a helper method called &#8220;callback&#8221; will generate them for you. Let&#8217;s redo the previous example, but this time with the help of &#8220;callback&#8221;.</p>
<div id="gist-1394390" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">add</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">var</span> <span class="n">addOne</span> <span class="o">=</span> <span class="n">callback</span><span class="o">(</span> <span class="n">add</span><span class="o">,</span> <span class="mi">1</span> <span class="o">);</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="n">addOne</span><span class="o">(</span> <span class="mi">5</span> <span class="o">);</span> <span class="c1">// returns 6</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1394390/0a2087420cb48b1f22401d430174aa7b019c48ee/simplePartialApplicationWithCallback.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1394390#file_simple_partial_application_with_callback.as" style="float:right;margin-right:10px;color:#666">simplePartialApplicationWithCallback.as</a>
            <a href="https://gist.github.com/1394390">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As you can see, this will save you from writing some code and at the same time it will also preserve type-safety. The &#8220;callback&#8221; method will also allow you to partially apply multiple parameters of your functions, it will apply them in the order they are defined in the function (read: left to right):</p>
<div id="gist-1394421" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">sumOfFloats</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">z</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">var</span> <span class="n">addFloatToOneAndTwo</span> <span class="o">=</span> <span class="n">callback</span><span class="o">(</span> <span class="n">sumOfFloats</span><span class="o">,</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">2</span> <span class="o">);</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="n">addFloatToOneAndTwo</span><span class="o">(</span> <span class="mi">5</span> <span class="o">);</span> <span class="c1">// returns 8</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1394421/19f4cbce09efafcd9f33d4fe707229f2124abb41/multipleParams.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1394421#file_multiple_params.as" style="float:right;margin-right:10px;color:#666">multipleParams.as</a>
            <a href="https://gist.github.com/1394421">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Use cases</h2>
<p>Partial function application can be very useful in a variety of cases. In functional programming they can actually be used to hold a state in situations where you don&#8217;t really need to create a class or value object to hold that state.</p>
<p>It will also be very useful when you need to pass a closure with only a subset of the parameters to a function, instead of doing the following:</p>
<div id="gist-1394538" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">doMath</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">operation</span><span class="o">:</span> <span class="kt">Float</span> <span class="err">-&gt;</span> <span class="err">Float</span> <span class="err">-&gt;</span> <span class="err">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">operation</span><span class="o">(</span> <span class="n">x</span><span class="o">,</span> <span class="n">y</span> <span class="o">);</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="kd">function </span><span class="nf">swapDivide</span><span class="o">(</span> <span class="n">swap</span><span class="o">:</span> <span class="kt">Bool</span><span class="o">,</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span>  <span class="o">{</span></div><div class='line' id='LC6'>	<span class="k">return</span> <span class="n">swap</span> <span class="o">?</span> <span class="n">y</span> <span class="sr">/ x : x /</span> <span class="n">y</span><span class="o">;</span></div><div class='line' id='LC7'><span class="o">}</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><span class="n">doMath</span><span class="o">(</span> <span class="mi">10</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="n">swapDivide</span><span class="o">(</span><span class="kc">true</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">);</span> <span class="o">}</span> <span class="o">);</span> <span class="c1">// returns 0.2</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1394538/1ebcd2369a94462843dc00277f09663a11fb27e9/gistfile1.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1394538#file_gistfile1.as" style="float:right;margin-right:10px;color:#666">gistfile1.as</a>
            <a href="https://gist.github.com/1394538">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>you can just replace the last line with this one:</p>
<div id="gist-1394542" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">doMath</span><span class="o">(</span> <span class="mi">10</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="n">callback</span><span class="o">(</span> <span class="n">swapDivide</span><span class="o">,</span> <span class="kc">true</span> <span class="o">)</span> <span class="o">);</span> <span class="c1">// return 0.2</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1394542/0a778237ea5a8fe148e852ec8e658ddbbd2fe5b5/gistfile1.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1394542#file_gistfile1.as" style="float:right;margin-right:10px;color:#666">gistfile1.as</a>
            <a href="https://gist.github.com/1394542">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Notes</h2>
<p>Please keep in mind that &#8220;partial application&#8221; gets mixed up a lot with &#8220;function currying&#8221;, also on the haXe mailing-list, but they are not the same. If you want to know what currying is you can <a href="http://en.wikipedia.org/wiki/Currying" target="_blank">read it up on Wikipedia</a>.</p>
<p>I am wondering why the method to create a partially applied function is called &#8220;callback&#8221;, which implies that it is kind of an async process or function like a promise or future. Shouldn&#8217;t it instead be called &#8220;papply&#8221;?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/25/partial-function-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding function types in haXe</title>
		<link>http://www.devboy.org/2011/11/22/understanding-function-types-in-haxe/</link>
		<comments>http://www.devboy.org/2011/11/22/understanding-function-types-in-haxe/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 01:54:54 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Learn haXe - the hard way]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=599</guid>
		<description><![CDATA[When you are an ActionScript 3 developer, like me, you might wonder while reading the title of this post: Function types? They&#8217;re just typed as Function! Oh, and types? Plural? There is only one kind of function in my code! That might be true for ActionScript, also JavaScript but in haXe you have a lot [...]]]></description>
			<content:encoded><![CDATA[<p>When you are an ActionScript 3 developer, like me, you might wonder while reading the title of this post: Function types? They&#8217;re just typed as Function! Oh, and types? Plural? There is only one kind of function in my code!<br />
That might be true for ActionScript, also JavaScript but in haXe you have a lot more information about your functions and more control over the type of them.</p>
<p>The haXe type-system might be one of the biggest differences to ActionScript 3 whereas the rest of the language and especially it&#8217;s syntax is very similar. To fully understand function types in haXe we first have to cover some ground on the haXe type-system itself and it&#8217;s features like type inference.</p>
<h2>Type inference</h2>
<p>Type inference does actually exactly what it name says, if you&#8217;re not a native English speaker like me, let&#8217;s have a look at the meaning of &#8220;inference&#8221; in the dictionary:</p>
<blockquote><p><strong>inference |ˈinf(ə)rəns|</strong></p>
<p>a conclusion reached on the basis of evidence and reasoning.</p></blockquote>
<p>Applying this to our haXe code will mean that the type of a variable will be concluded on the basis of evidence, but what is the evidence in this case? Pretty simple, it&#8217;s the first value you assign to the variable.<br />
Let&#8217;s have a look at some code:</p>
<div id="gist-1384355" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">x</span><span class="o">;</span> <span class="c1">// type of x is Unknown</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">x</span> <span class="o">=</span> <span class="s2">&quot;I&#39;m a String&quot;</span><span class="o">;</span> <span class="c1">// type of x is set to String	</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// this will cause a compiler error as Int cannot be assigned to String</span></div><div class='line' id='LC4'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384355/aae3bad89e3817088fadfc1c50755aff1b576651/typeInf.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384355#file_type_inf.as" style="float:right;margin-right:10px;color:#666">typeInf.as</a>
            <a href="https://gist.github.com/1384355">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You can see that once you assign a value, the variables type is fixed to the type of the assigned value. This will save you some typing, especially for local variables, while in other languages like ActionScript 3 you would have to set the type manual for each variable.<br />
Type inference is not mandatory, you can of course type your variables before you assign a value to them, just like in ActionScript:</p>
<div id="gist-1384444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="n">x</span><span class="p">:</span> <span class="kt">String</span><span class="o">;</span> <span class="c1">// type is now set to String</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">x</span> <span class="o">=</span> <span class="s2">&quot;I&#39;m a String&quot;</span><span class="o">;</span> <span class="c1">// this will work</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// this will cause an error</span></div><div class='line' id='LC4'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384444/407f29f17b5aa13754db80aba56e8f8e1f3ef7e0/manualTyping.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384444#file_manual_typing.as" style="float:right;margin-right:10px;color:#666">manualTyping.as</a>
            <a href="https://gist.github.com/1384444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>There are still a lot of use cases for setting types manually, like typing a variable to an interface.</p>
<p>Types will also be inferred for function parameters and return values:</p>
<div id="gist-1384465" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">inferred</span><span class="o">(</span><span class="err">x</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">x</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">inferred</span><span class="o">(</span><span class="s2">&quot;abc&quot;</span><span class="o">);</span> <span class="c1">// will take the string and return it</span></div><div class='line' id='LC6'><span class="n">inferred</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span> <span class="c1">// will throw a compiler error because x is typed to String now </span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384465/4eb0aa4044931bbba135692a5931a96af935c237/inferredFunctionParam.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384465#file_inferred_function_param.as" style="float:right;margin-right:10px;color:#666">inferredFunctionParam.as</a>
            <a href="https://gist.github.com/1384465">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Function types</h2>
<p>Now that we know that haXe can infer the type of function parameters and return values, we can finally start talking about function types. As for type inference, parameter and return types are the building blocks of the type of a function, let&#8217;s have a look at a simple square function that takes a number and multiplies it by itself.</p>
<div id="gist-1384493" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">square</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384493/ea9e69478a81099fa2f341cb335653e15fa464dc/square.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384493#file_square.as" style="float:right;margin-right:10px;color:#666">square.as</a>
            <a href="https://gist.github.com/1384493">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>We&#8217;ve set the parameter &#8220;x&#8221; to the type Float, and the type of the return value will be inferred to Float as well. Let&#8217;s store it in a local variable and ask the haXe compiler for its type:</p>
<div id="gist-1384527" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">square</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span> <span class="n">Float</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="o">;</span> </div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">type</span><span class="o">(</span><span class="n">square</span><span class="o">);</span> <span class="c1">// type of square is Float -&gt; Float</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384527/4e1f1c6312784561a1302e0c761a4125f6bba8c1/type.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384527#file_type.as" style="float:right;margin-right:10px;color:#666">type.as</a>
            <a href="https://gist.github.com/1384527">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>So the type is <strong>Float -&gt; Float </strong>, let&#8217;s decipher this: The first Float is the parameter x and the second Float is the return value which is type inferred to Float. To make this a little clearer, let&#8217;s have a look at a function that takes two parameters:</p>
<div id="gist-1384544" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">getStringXTimes</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span> <span class="n">Int</span><span class="o">,</span> <span class="n">string</span><span class="o">:</span> <span class="n">String</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="n">var</span> <span class="n">result</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">;</span></div><div class='line' id='LC3'>	<span class="k">for</span> <span class="o">(</span> <span class="n">i</span> <span class="k">in</span> <span class="mi">0</span><span class="o">...</span><span class="n">x</span> <span class="o">)</span></div><div class='line' id='LC4'>		<span class="n">result</span> <span class="o">+=</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC5'>	<span class="k">return</span> <span class="n">result</span><span class="o">;</span> </div><div class='line' id='LC6'><span class="o">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="n">type</span><span class="o">(</span><span class="n">getStringXTimes</span><span class="o">);</span> <span class="c1">// Int -&gt; String -&gt; String</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384544/9db4c41e18a09328ed9e6870798c1401d87efcb9/getStringXTimes.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384544#file_get_string_x_times.as" style="float:right;margin-right:10px;color:#666">getStringXTimes.as</a>
            <a href="https://gist.github.com/1384544">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now the type is <strong>Int -&gt; String -&gt; String  </strong>, which let&#8217;s us conclude that list of types seperated by <strong>-&gt; </strong>contains of all parameter types and ends with the type of the return value.</p>
<p>This lets us now do something very useful, we can now set our variable to a certain function type, for example a type that taks an Int parameter an returns a Bool:</p>
<div id="gist-1384559" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="n">biggerThan2</span><span class="p">:</span> <span class="kt">Int</span> <span class="o">-&gt;</span> <span class="n">Bool</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="o">;</span></div><div class='line' id='LC3'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384559/66d1ef172652d37a1ce07c79a6d44a8bf10982b9/typedFunction.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384559#file_typed_function.as" style="float:right;margin-right:10px;color:#666">typedFunction.as</a>
            <a href="https://gist.github.com/1384559">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This will now give us compile time checking for function types, so no stumbling in the dark when it comes to passing functions around!</p>
<div id="gist-1384581" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">doMath</span><span class="o">(</span> <span class="n">x</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="kt">Float</span><span class="o">,</span> <span class="n">mathOperation</span><span class="o">:</span> <span class="kt">Float</span> <span class="err">-&gt;</span> <span class="err">Float</span> <span class="err">-&gt;</span> <span class="err">Float</span> <span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">return</span> <span class="n">mathOperation</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">);</span></div><div class='line' id='LC3'><span class="o">}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">var</span> <span class="n">addition</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span> <span class="n">Float</span><span class="o">,</span> <span class="n">y</span><span class="o">:</span> <span class="n">Float</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC6'>	<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">;</span></div><div class='line' id='LC7'><span class="o">}</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><span class="n">var</span> <span class="n">square</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span> <span class="n">Float</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC10'>	<span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="o">;</span></div><div class='line' id='LC11'><span class="o">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="n">type</span><span class="o">(</span> <span class="n">doMath</span> <span class="o">);</span> <span class="c1">// Float -&gt; Float -&gt; (Float -&gt; Float -&gt; Float) -&gt; Float </span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="n">doMath</span><span class="o">(</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="n">addition</span> <span class="o">);</span> <span class="c1">// will return 3</span></div><div class='line' id='LC16'><span class="n">doMath</span><span class="o">(</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="n">square</span> <span class="o">);</span> <span class="c1">// compiler error: Float -&gt; Float should be Float -&gt; Float -&gt; Float</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384581/35b3649b66f28bde96436e9122e8fcd5311082c0/functionTypes.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384581#file_function_types.as" style="float:right;margin-right:10px;color:#666">functionTypes.as</a>
            <a href="https://gist.github.com/1384581">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>That&#8217;s just plain awesome! Isn&#8217;t it? We can now say goodbye to runtime errors!</p>
<h2>Type parameters</h2>
<p>As haxe has <a href="http://alecmce.com/as3/why-we-need-generics-in-as3" target="_blank">support for generics</a>, you might want to type some of your parameters or the return value dynamically, depending on the type of a parameter. Let&#8217;s say we want to create a function that deletes items of an array based on the result of a function you pass as a parameter. Arrays in haXe are like Vectors in ActionScript 3, it can only contain items of a single type.<br />
The first idea would be to just assign a dynamic type to your function parameters, like you would do in ActionScript 3 (Dynamic is equal to the * type in AS3):</p>
<div id="gist-1384646" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function </span><span class="nf">array_delete_if</span><span class="o">(</span> <span class="n">array</span><span class="o">:</span> <span class="kt">Array</span><span class="err">&lt;Dynamic&gt;,</span> <span class="n">processor</span><span class="o">:</span> <span class="kt">Dynamic</span><span class="err">-&gt;</span> <span class="err">Bool</span> <span class="o">):</span><span class="kt">Array</span><span class="o">&lt;</span><span class="n">Dynamic</span><span class="o">&gt;</span></div><div class='line' id='LC2'><span class="o">{</span></div><div class='line' id='LC3'>	<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">array</span> <span class="o">)</span></div><div class='line' id='LC4'>		<span class="k">if</span> <span class="o">(</span> <span class="n">processor</span><span class="o">(</span><span class="n">item</span><span class="o">)</span> <span class="o">)</span></div><div class='line' id='LC5'>			<span class="n">array</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC6'>	<span class="k">return</span> <span class="n">array</span><span class="o">;</span></div><div class='line' id='LC7'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384646/91c6495968aaf78d4efa35a63c31c801f8d7623b/dynamicArray.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384646#file_dynamic_array.as" style="float:right;margin-right:10px;color:#666">dynamicArray.as</a>
            <a href="https://gist.github.com/1384646">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>But this is not very useful as we are gonna loose compile time checking. For example when passing a function that takes an Int as parameter instead of the String that is in the array. But haXe wouldn&#8217;t be haXe if it wouldn&#8217;t provide us with a nice solution: We can add a type parameter to the function which will resolve the type of the values stored in the array, store it in the value T, which then can be used to describe the other types of the function parameters and return value.</p>
<p>With this we will get nice compile time checking for our function!</p>
<div id="gist-1384637" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">var</span> <span class="n">ints</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="o">]</span></div><div class='line' id='LC2'><span class="n">var</span> <span class="n">strings</span> <span class="o">=</span> <span class="o">[</span><span class="s2">&quot;a&quot;</span><span class="o">,</span> <span class="s2">&quot;bb&quot;</span><span class="o">,</span> <span class="s2">&quot;ccc&quot;</span><span class="o">];</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="n">var</span> <span class="n">biggerThan1</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span><span class="n">Int</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC5'>	<span class="k">return</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="o">;</span></div><div class='line' id='LC6'><span class="o">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="n">var</span> <span class="n">longerThan1</span> <span class="o">=</span> <span class="kd">function</span><span class="o">(</span><span class="n">x</span><span class="o">:</span><span class="n">String</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC9'>	<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">length</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="o">;</span></div><div class='line' id='LC10'><span class="o">}</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="kd">function</span> <span class="n">array_delete_if</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;(</span> <span class="n">array</span><span class="o">:</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;,</span> <span class="n">processor</span><span class="o">:</span> <span class="n">T</span><span class="o">-&gt;</span> <span class="n">Bool</span> <span class="o">):</span><span class="n">Array</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span></div><div class='line' id='LC13'><span class="o">{</span></div><div class='line' id='LC14'>	<span class="k">for</span> <span class="o">(</span> <span class="n">item</span> <span class="k">in</span> <span class="n">array</span> <span class="o">)</span></div><div class='line' id='LC15'>		<span class="k">if</span> <span class="o">(</span> <span class="n">processor</span><span class="o">(</span><span class="n">item</span><span class="o">)</span> <span class="o">)</span></div><div class='line' id='LC16'>			<span class="n">array</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span> <span class="n">item</span> <span class="o">);</span></div><div class='line' id='LC17'>	<span class="k">return</span> <span class="n">array</span><span class="o">;</span></div><div class='line' id='LC18'><span class="o">}</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'><span class="n">array_delete_if</span><span class="o">(</span><span class="n">ints</span><span class="o">,</span> <span class="n">biggerThan1</span><span class="o">);</span> <span class="c1">// [ 1 ]</span></div><div class='line' id='LC21'><span class="n">array_delete_if</span><span class="o">(</span><span class="n">strings</span><span class="o">,</span> <span class="n">longerThan1</span><span class="o">);</span> <span class="c1">// [ &quot;a&quot; ]</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1384637/8827f3dfd11e1903715f19043e15632ee11f3e16/typeParams.as" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1384637#file_type_params.as" style="float:right;margin-right:10px;color:#666">typeParams.as</a>
            <a href="https://gist.github.com/1384637">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/22/understanding-function-types-in-haxe/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>gotoAndSki 2012: The productive programmer</title>
		<link>http://www.devboy.org/2011/11/21/gotoandski-2012-the-productive-programmer/</link>
		<comments>http://www.devboy.org/2011/11/21/gotoandski-2012-the-productive-programmer/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 20:17:11 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=579</guid>
		<description><![CDATA[I am more than delighted to be speaking at the upcoming gotoAndSki conference in Switzerland. The conference will be held in the lovely town of Stechelberg starting January the 26th and lasting for three full days. The concept of this conference is a little different from the &#8220;usual&#8221; conferences, instead of sitting in a dark [...]]]></description>
			<content:encoded><![CDATA[<p>I am more than delighted to be speaking at the upcoming <a href="http://switzerland.gotoandski.com/" target="_blank">gotoAndSki conference</a> in Switzerland. The conference will be held in the lovely town of Stechelberg starting January the 26th and lasting for three full days. The concept of this conference is a little different from the &#8220;usual&#8221; conferences, instead of sitting in a dark room all day you will be hitting the mountains for some ski or snowboard action during the day, and the sessions will be held in the afternoon with some delicious Swiss dinner in between.<br />
This years speaker lineup is outstanding too, I am very proud to be part of a group that includes household names like: Mario Klingemann, Eugene Zatepyakin, Michael Plank, Peter Elst, Mihai Corlan, Hugo Fernandes, Bhakti Pingale and Steven Peeters! You can find the <a href="http://switzerland.gotoandski.com/sessions-speakers/" target="_blank">lineup with session information here</a>.<br />
By the way: Tickets seem to sell quicker than ever, so make sure to get yours in time!</p>
<h2>Session: The productive programmer</h2>
<p>In our industry we always need to produce better, faster and cheaper. Therefore we always need to be on the hunt for better practices, processes and tools! That is where &#8220;the productive programmer&#8221; comes into play.</p>
<p>A productive programmer &#8230;</p>
<ul>
<li>&#8230; obeys the DRY principle: Don&#8217;t repeat yourself!</li>
<li>&#8230; uses proven best practices and processes.</li>
<li>&#8230; has the ability to choose the right tools for the job.</li>
<li>&#8230; will create the right tool for the job if necessary.</li>
<li>&#8230; will always question the way he works rather than blindly adhering to standards.</li>
</ul>
<p>In the session we will shed some light on topics like automation, automatic code-generation, tool-building and test-driven development. We will also look into processes and methodologies from the enterprise as well as the indie-dev to pick the best from both worlds.</p>
<p>I hope that after this you&#8217;ll be a productive programmer too, or if you already are, an even more productive one!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/21/gotoandski-2012-the-productive-programmer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>haXe, haxelib and buildr-hx</title>
		<link>http://www.devboy.org/2011/11/18/haxe-haxelib-and-buildr-hx/</link>
		<comments>http://www.devboy.org/2011/11/18/haxe-haxelib-and-buildr-hx/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 14:30:32 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=505</guid>
		<description><![CDATA[I have been dabbling with haXe again in the past couple of days which is always a lot of fun. The feature-rich programming language allows for simple yet powerful code and the ability to target a variety of platforms makes it a development-tool everybody should consider. But I don&#8217;t want to talk too much about [...]]]></description>
			<content:encoded><![CDATA[<p>I have been dabbling with haXe again in the past couple of days which is always a lot of fun. The feature-rich programming language allows for simple yet powerful code and the ability to target a variety of platforms makes it a development-tool everybody should consider.<br />
But I don&#8217;t want to talk too much about haXe itself or even the haXe programming-language. I want to talk about a tool shipped with haXe called <a href="http://haxe.org/doc/haxelib/using_haxelib" target="_blank">haxelib</a> and how it plays a role in my buildr-plugin <a href="https://github.com/devboy/buildr-hx" target="_blank">buildr-hx</a>.</p>
<h2>What is buildr-hx?</h2>
<p>Simply put, buildr-hx is a plugin for the buildr build-system to support haXe development. Buildr is an amazing tool to automate your compile, testing and deploy (and a lot more!) tasks and is not only feature-equivalent to build-systems like Ant or Maven but also provides you with a simple and clean API which makes it possible to describe even the most complex projects in a few lines of code. The ability to easily download 3rd-party libraries from Maven repositories makes it even more awesome!<br />
There is a lot more to it, but I got to keep it short. Please check out the <a href="http://buildr.apache.org/" target="_blank">buildr project</a> or the <a href="https://github.com/devboy/buildr_as3/wiki" target="_blank">wiki-pages</a> of my buildr-plugin for AS3 and Flex for more information.</p>
<h2> What is haxelib?</h2>
<p>Haxelib is a command-line-tool to dowload and share haXe libraries via a central repository at <a href="http://lib.haxe.org" target="_blank">lib.haxe.org</a>. This makes it really easy to use a library like nodejs in your haXe projects, you first install the library with haxelib:</p>
<pre>$ haxelib install nodejs 0.6</pre>
<p>and then just tell the haXe compiler that you want to use it in your project:</p>
<pre>$ haxe -lib nodejs:0.6</pre>
<p>Uploading your own library is almost as simple, you just throw your source-code and a descriptor-file into a zip archive that you can upload to the haxelib repository:</p>
<pre>$ haxelib submit myLibrary.zip</pre>
<p>The descriptor-file, called haxelib.xml, contains all the necessary information about the library like: name, description, version and dependencies to other libraries. Furthermore you can also attach documentation or executable neko scripts to your library package.</p>
<p>So far, so good. A tool like this makes a developers life a lot easier and was really missing in the Flash &amp; Flex space, even though it can be done with Maven, Ivy or Buildr and even though there have been several attempts to introduce this it never gained ground.</p>
<h2>How can haxelib be improved?</h2>
<p>Even though I am very enthusiastic about haxelib and really like the idea, I have experienced some issues and see some potential problems that could arise, especially in the context of multi-module projects or bigger teams of developers. Here I will list the issues I came across and will try to shed some light on their problems and possible improvements.</p>
<h3>Modify the location of the local haxelib repository</h3>
<p><img class="alignnone size-full wp-image-553" title="" src="http://www.devboy.org/wp-content/uploads/2011/11/Screen-Shot-2011-11-18-at-1.44.30-PM.png" alt="" width="700" height="100" /><br />
By default haxelib will download libraries into a &#8220;global&#8221; directory, on OSX that would be: /usr/lib/haxe/lib/. Personally I do prefer to rather download the libraries into a directory under my home directory, so I tried to change the path of haxelib:</p>
<pre> $ haxelib setup ~/.haxe/lib</pre>
<p>But this doesn&#8217;t work at all and fails with an error:</p>
<pre>Please enter haxelib repository path with write access
Hit enter for default (/usr/lib/haxe/lib)
Failed to create directory '/Users/devboy/.haxe/lib' (std@sys_create_dir),
maybe you need appropriate user rights</pre>
<p>This is not an issue with user rights, as I do of course have access to my home directory and also a sudo won&#8217;t help. It seems that haxelib doesn&#8217;t allow any custom paths to be used as repository locations, as I have tried several without success.<br />
This is problematic because it doesn&#8217;t allow you to have multiple haxelib locations side by side which can be very useful in some cases. It also won&#8217;t allow you to have different haxelib setups for different users which can cause trouble when for example running a continous-integration system like jenkins on the same machine that you develop on. Basically any system where you need to have different configurations side by side!</p>
<p>[Edit]</p>
<p>As Nicolas Canasse pointed out in the comments, when first creating the directory by hand it will work. But still hope this will be fixed in a future version.</p>
<h3>Haxelib install should also install haxelib zip archives</h3>
<p><img class="alignnone size-full wp-image-558" title="" src="http://www.devboy.org/wp-content/uploads/2011/11/Screen-Shot-2011-11-18-at-1.58.49-PM.png" alt="" width="700" height="70" /></p>
<p>The haxelib install command will only install libraries hosted on lib.haxe.org and not library archives you have on you filesystem. You&#8217;re supposed to use the haxelib test command to install library archives into your local repository. This works fine, but in my opinion the name is misleading and it should be part of the install command.</p>
<h3>Host your own haxelib server</h3>
<p><img class="alignnone size-full wp-image-556" title="lib.haxe" src="http://www.devboy.org/wp-content/uploads/2011/11/lib.haxe_.jpg" alt="" width="700" height="70" /><br />
The centralized repository  at lib.haxe.org is a great way to distribute open-source projects. But what about closed-source or internal projects within your company or team of developers?<br />
We need a way to host our own repositories so we can leverage haxelib throughout the whole development process, not only for open-source!</p>
<h3>Haxe compiler should accept haxelib zip archives as classpath</h3>
<p><img class="alignnone size-full wp-image-562" title="" src="http://www.devboy.org/wp-content/uploads/2011/11/Screen-Shot-2011-11-18-at-2.06.30-PM.png" alt="" width="700" height="100" /></p>
<p>The haxe compiler does not allow for haxelib zip archives to be used as classpaths. This doesn&#8217;t seem to be an issue at first as you can just install them with haxelib and then use the -lib parameter of the compiler. But what if you don&#8217;t retrieve your libraries from lib.haxe.org? And what if you don&#8217;t want these libs to be installed into your local haxelib repository? You&#8217;ll always need to unpack the archive and point the compiler to it&#8217;s contents, which can be a tedious task even when automated.<br />
The heart of the problem lies a little bit deeper: haXe does not have a library format similar to a JAR in Java or a SWC in AS3. Therefore it is not really possible to distribute haXe libraries in any other way than as source-code or via lib.haxe.org. It is essential to at least have the option to deploy a haXe library to, for example, a Maven repository, so it can be easily integrated into existing systems and workflows.<br />
Actually a haxelib zip archive could easily work as a library format, and if either the haXe compiler or haxelib would provide a tool to easily create these archives, so they are all following the same convention, it should also be possible to use these libraries as classpaths in the compiler.</p>
<h3>Haxelib non-interactive-modes for configuration</h3>
<p><img class="alignnone size-full wp-image-568" title="" src="http://www.devboy.org/wp-content/uploads/2011/11/Screen-Shot-2011-11-18-at-2.49.55-PM.png" alt="" width="700" height="84" /></p>
<p>Who doesn&#8217;t like interactive-modes in command-line applications? I love them, it&#8217;s so much easier to walk through a dialog then having to remember all the correct parameters.<br />
But what if it&#8217;s not a human that has to go through the dialog, what if you want to code against the command-line-interface?<br />
I have this problem when trying to automate haXe unit-testing with buildr-hx and I need to provide it with some paths to be able to run the unit-tests, it&#8217;s a pain in the ass to try and step through a dialog like this via code, trying to parse stdin &amp; stdout etc.<br />
Always, I really mean always, provide a way to set all the values outside of a dialog!</p>
<h3>Favor executables over haxelib run</h3>
<p>When a haXe lib includes a neko run script, it can be executed via the haxelib run command, just like munit previously:</p>
<pre>$ haxelib run munit</pre>
<p>An idea would be to install an executable instead of  using haxelib run. That way haxelib could provide a powerful eco-system for (not only) command-line tools, just like Rubygems provides!<br />
I know that you can use aliases to shortcut the command, but thats not quite the same.</p>
<h2>Conclusion</h2>
<p>Haxelib is a great tool, but to really prove effective for my daily workflow I would  need the improvements mentioned above. I am confident that this will happen in some way, as haXe development moves very quickly and user input seems to play an important role of feature development.<br />
Please feel free to share this post and join a discussion in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/11/18/haxe-haxelib-and-buildr-hx/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Build-system survey: The numbers!</title>
		<link>http://www.devboy.org/2011/07/18/build-system-survey-the-numbers/</link>
		<comments>http://www.devboy.org/2011/07/18/build-system-survey-the-numbers/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 22:20:27 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.devboy.org/?p=479</guid>
		<description><![CDATA[While I was preparing my presentation at the FlexUserGroup in Amsterdam about buildr and build-systems in general I wanted to know what the weapons of choice are in the Flex &#38; Flash developer community. So I created a really simple survey, just three short questions, and asked the community to participate. A couple of days [...]]]></description>
			<content:encoded><![CDATA[<p>While I was preparing my presentation at the <a href="http://www.flugr.nl/" target="_blank">FlexUserGroup in Amsterdam</a> about <a href="http://buildr.apache.org/" target="_blank">buildr</a> and build-systems in general I wanted to know what the weapons of choice are in the Flex &amp; Flash developer community. So I created a really simple survey, just three short questions, and asked the community to participate. A couple of days later, right before my presentation, 140 developers already took part in the survey and provided me with a nice set  of data to integrate into my presentation.<br />
Without further adieu: The results of this survey.</p>
<h2> What build-system are you using?</h2>
<p><img class="alignnone size-full wp-image-483" title="" src="http://www.devboy.org/wp-content/uploads/2011/07/Screen-shot-2011-07-18-at-11.05.36-PM.png" alt="" width="700" height="326" /></p>
<p>Exactly <strong>70%</strong> percent are not relying on any build-system at all and use the compile functionalities of their IDE&#8217;s (Flash Builder, Flash Pro, Flash Develop, etc.) instead. The <a href="http://ant.apache.org/" target="_blank">Apache Ant</a> build tool comes in second with <strong>24 users</strong>. This accounts for <strong>more than 50%</strong> of the developers not using the integrated tools of their IDE. <a href="http://maven.apache.org/" target="_blank">Maven</a>, also maintained by Apache, is the build tool of choice for <strong>8 developers</strong>. Let&#8217;s skip the others for now as a single user does not represent a user-base.</p>
<h2>What build-system did you already use?</h2>
<p><img class="alignnone size-full wp-image-484" title="" src="http://www.devboy.org/wp-content/uploads/2011/07/Screen-shot-2011-07-18-at-11.06.02-PM.png" alt="" width="700" height="366" /></p>
<p>Ant is the most popular build-tool among developers as <strong>more than 80%</strong> already have experience with it. Maven comes in second, but is no close to Ant, with <strong>nearly 20%</strong>. Worth a mention under the follow-ups is <a href="http://rake.rubyforge.org/" target="_blank">Rake</a>, a <a href="http://www.ruby-lang.org/" target="_blank">ruby</a> based build-system. As <a href="http://projectsprouts.org/" target="_blank">project-sprouts</a> also uses rake as build-tool we can add them together to <strong>13 users</strong> which adds up to <strong>about 10%</strong> of all participants.</p>
<h2>What build-system are you interested in using?</h2>
<p><img class="alignnone size-full wp-image-485" title="" src="http://www.devboy.org/wp-content/uploads/2011/07/Screen-shot-2011-07-18-at-11.06.14-PM.png" alt="" width="700" height="367" /></p>
<p>The <strong>74 developers</strong> answering  &#8221;None&#8221; in this question indicate that a little more than 50% are happy with their current setup. What about the other 50%? Maven seems to be the tool that most developers want to give a try in the future. <strong>Around 20%</strong> chose it. On second place is, which makes me really happy, buildr which supports ActionScript3 &amp; Flex through the <a href="https://github.com/devboy/buildr_as3" target="_blank">buildr-as3 plugin</a> I developed. Almost as many votes go to Ant.</p>
<h2>Conclusion</h2>
<p>The first thing I noticed was that the majority of developers is not using any build-system at all. This made me wonder but also motivated me to write a blog-post on why you should use a build-system and what you and your team can gain from it. I will publish it here very soon.</p>
<p>Ant is by far the most popular build-tool out there, so if you want to make sure other developers can get started quickly you should go for Ant.</p>
<p>The Flex &amp; Flash community is really missing out on some powerful features that project-oriented build-tools like Maven, Ivy or buildr provide. <a href="http://en.wikipedia.org/wiki/Package_management_system" target="_blank">Dependency-management</a> via public repositories for example is a really nice way to distribute you libraries and tools to other developers.</p>
<p>After all I am pleased to see that a few developers want to go and try out different systems which might make one tool gain more momentum in the future. But I doubt that anything will kick Ant from its throne anytime soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/07/18/build-system-survey-the-numbers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Build-System survey for Flash and Flex Developers</title>
		<link>http://www.devboy.org/2011/04/20/build-system-survey-for-flash-and-flex-developers/</link>
		<comments>http://www.devboy.org/2011/04/20/build-system-survey-for-flash-and-flex-developers/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 11:10:25 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://devboy.org/?p=401</guid>
		<description><![CDATA[Please help me with my research and answer the following questions. You&#8217;ll be done in a minute. I promise! The survey has been closed. I will publish the survey results in a following post, thanks to everyone who took part.]]></description>
			<content:encoded><![CDATA[<p>Please help me with my research and answer the following questions. You&#8217;ll be done in a minute. I promise!</p>
<p><strong>The survey has been closed.</strong></p>
<p>I will publish the survey results in a following post, thanks to everyone who took part.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/04/20/build-system-survey-for-flash-and-flex-developers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>buildr-as3 0.2 &#8211; getting advanced</title>
		<link>http://www.devboy.org/2011/04/18/buildr-as3-0-2-getting-advanced-2/</link>
		<comments>http://www.devboy.org/2011/04/18/buildr-as3-0-2-getting-advanced-2/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 14:02:29 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://devboy.org/?p=392</guid>
		<description><![CDATA[The past days I have been working on buildr-as3. The goal was to complete the current feature-set and fix it’s bugs in order to bring the codebase into a state where I could finally make a stable dot-release. The product of these efforts is buildr-as3 0.2. This release comes with all the features needed for [...]]]></description>
			<content:encoded><![CDATA[<p>The past days I have been working on buildr-as3. The goal was to complete the current feature-set and fix it’s bugs in order to bring the codebase into a state where I could finally make a stable dot-release. The product of these efforts is buildr-as3 0.2.<br />
This release comes with all the features needed for a full development and build lifecycle: compilation, documentation, testing, optimization, packaging and releasing. In addition you will profit from the updated API which is now more consistent with the buildr API.</p>
<p>To get you started with the new features of buildr-as3, I’ll go through a simple step-by-step tutorial which will cover compilation, optimization, documentation and testing.</p>
<h2>Step 1 &#8211; Installing or updating the buildr-as3 gem</h2>
<p>If you have no previous installation of <a href="http://buildr.apache.org" target="_blank">buildr</a> or buildr-as3 installed, please follow the instructions on <a href="http://buildr.apache.org/installing.html" target="_blank">http://buildr.apache.org/installing.html</a> and complete by installing the buildr-as3 gem:<br />
<script type="text/javascript" src="https://gist.github.com/925292.js?file=gistfile1.sh"></script>In case you just need to update the buildr-as3 gem, you’ll be up and running after executing:<script type="text/javascript" src="https://gist.github.com/925295.js?file=gistfile1.sh"></script><br />
You should see output similar to this:<br />
<script type="text/javascript" src="https://gist.github.com/925297.js?file=gistfile1.sh"></script></p>
<h2>Step 2 &#8211; Creating a buildfile</h2>
<p>Let’s get started with buildr! Before we start writing a buildfile let’s have a look at our project directory-structure:<script type="text/javascript" src="https://gist.github.com/925299.js?file=gistfile1.txt"></script></p>
<p>“AdvancedExample” is the name of our project and it contains one module which is called “MyApplication”. When you’re working with eclipse you might translate project with workspace and module with project to get a better understanding of this setup.<br />
Our module “MyApplication” contains a source folder in “src/main/as3” with just one file: “MyApplication.as”.<br />
In order to compile this module we need to specify the module and its settings, such as compiler, main-file and other options, in our buildfile.<br />
Let’s create a file called “buildfile” in our project-folder, like this:</p>
<p><script type="text/javascript" src="https://gist.github.com/925301.js?file=gistfile1.txt"></script>Now let’s start writing some Ruby-code to define our build, first we are going to outline our project:<script type="text/javascript" src="https://gist.github.com/925303.js?file=gistfile1.txt"></script></p>
<p>Now we need to make some settings for the compile-task:</p>
<p><script type="text/javascript" src="https://gist.github.com/925305.js?file=gistfile1.txt"></script>Thats all we need to compile a simple ActionScript3 project into a swf, you now just need to go into the terminal:<script type="text/javascript" src="https://gist.github.com/925310.js?file=gistfile1.sh"></script></p>
<p>Now we compiled a swf-file of our module, not to complicated was it?<br />
Let’s move on to some more advanced stuff.</p>
<h2>Step 3 &#8211; Optimize your swf-file with Apparat</h2>
<p>I am sure you have heard of <a href="http://apparat.googlecode.com" target="_blank">Apparat</a>, the infamous framework by <a href="http://blog.joa-ebert.com/" target="_blank">Joa Ebert</a> to optimize the bytecode of your flash-application to gain some extra performance.<br />
In case you never managed to get it running or integrate into your build don’t worry,<br />
buildr-as3 will optimize your swf with just two additional lines of code:</p>
<p><script type="text/javascript" src="https://gist.github.com/925311.js?file=gistfile1.rb"></script>This will execute <a href="http://blog.joa-ebert.com/2009/08/05/tdsi-examples/" target="_blank">TDSI</a>after your swf is compiled, the best is that you don’t need to call a special task! First we’ll have to clean the project to force a recompile:<script type="text/javascript" src="https://gist.github.com/925313.js?file=gistfile1.sh"></script></p>
<p>Finally we can compile with Apparat. (Make sure Scala is installed and on your path)</p>
<p><script type="text/javascript" src="https://gist.github.com/925315.js?file=gistfile1.sh"></script>There we go, now we got Apparat optimization every time we compile.</p>
<h2>Step 4 &#8211; Unit Testing with FlexUnit4</h2>
<p>In case you want to unit-test your code, buildr-as3 has an integration with <a href="http://www.flexunit.org/" target="_blank">flexunit4</a>to make unit-testing as easy as possbile. To start testing we need to create our TestSuites and the flexunit4 TestRunner in the following directory structure:<script type="text/javascript" src="https://gist.github.com/925318.js?file=gistfile1.txt"></script></p>
<p>Of course we need to let buildr know that we want to unit-test our code,<br />
you need to change your buildfile to look like this:</p>
<p><script type="text/javascript" src="https://gist.github.com/925320.js?file=gistfile1.rb"></script>To run the unit-tests with flexunit4 we just need to run:<script type="text/javascript" src="https://gist.github.com/925322.js?file=gistfile1.sh"></script></p>
<p>Additional to the output in the terminal about the success or failure of the test, an html-page with the results will be generated by default.</p>
<p>I’ll stop here, knowing that I could have gone into much more detail about these features. I will cover the contents of this article in more depth in a screencast which i’ll do very soon. If you got further questions please feel free to use the comments, ask me on <a href="http://twitter.com/#!/devboy_org" target="_blank">twitter</a> or use the <a href="http://questions.devboy.org/" target="_blank">questions &amp; answers page</a>, thanks.</p>
<p>By the way: You can now find this <a href="https://github.com/devboy/buildr_as3_advanced_example" target="_blank">example project on github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/04/18/buildr-as3-0-2-getting-advanced-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cellular automatons</title>
		<link>http://www.devboy.org/2011/02/16/cellular-automatons/</link>
		<comments>http://www.devboy.org/2011/02/16/cellular-automatons/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 12:02:40 +0000</pubDate>
		<dc:creator>devboy</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://devboy.org/?p=350</guid>
		<description><![CDATA[The last day I played around with cellular automatons, just for fun. If you know Conway&#8217;s game of life than you know what a cellular automaton is, but if you want to learn more about them check out the wikipedia page and watch the talk by Stephen Wolfram: A new kind of science. You can [...]]]></description>
			<content:encoded><![CDATA[<p>The last day I played around with cellular automatons, just for fun. If you know <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life" target="_blank">Conway&#8217;s game of life</a> than you know what a cellular automaton is, but if you want to learn more about them check out the <a href="http://en.wikipedia.org/wiki/Cellular_automaton" target="_blank">wikipedia page</a> and watch the talk by <a href="http://en.wikipedia.org/wiki/Stephen_Wolfram" target="_blank">Stephen Wolfram</a>: <a href="http://www.youtube.com/watch?v=_eC14GonZnU" target="_blank">A new kind of science</a>.</p>
<p>You can run it in two different modes: LINEAR which runs from top to bottom and CIRCULAR which runs from center in against-the-clock rotation. I added a few rules mentioned by Stephen Wolfram which I found interesting, like <a href="http://en.wikipedia.org/wiki/Rule_30" target="_blank">Rule 30</a>. Additionally I made up some rules myself.</p>
<p>I had some interesting results while playing around but unfortunately can&#8217;t reproduce them right now. Luckily i tweeted some screenshots <a href="https://twitter.com/#!/devboy_org/status/37288281498460160" target="_blank">here</a> &amp; <a href="https://twitter.com/#!/devboy_org/status/37288281498460160" target="_blank">here</a>.</p>
<p>Play around with this basic automaton library, i&#8217;ll try to add more rules in the future.</p>
<p><object width="601" height="601" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://devboy.org/wp-content/uploads/CellularAutomatons.swf" /><embed width="601" height="601" type="application/x-shockwave-flash" src="http://devboy.org/wp-content/uploads/CellularAutomatons.swf" /></object></p>
<p>Thanks go out to <a href="http://twitter.com/#!/soulwire" target="_blank">@soulwire</a> for his <a href="http://blog.soulwire.co.uk/code/actionscript-3/simple-flash-prototype-gui-tool" target="_blank">SimpleGUI</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devboy.org/2011/02/16/cellular-automatons/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

