<?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>n3rd.org &#187; programming</title>
	<atom:link href="http://www.n3rd.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.n3rd.org</link>
	<description>random thoughts on not-so-random things</description>
	<lastBuildDate>Sun, 28 Feb 2010 22:29:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>On The Market</title>
		<link>http://www.n3rd.org/2009/09/09/on-the-market/</link>
		<comments>http://www.n3rd.org/2009/09/09/on-the-market/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 16:39:46 +0000</pubDate>
		<dc:creator>n3rd</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.n3rd.org/?p=1425</guid>
		<description><![CDATA[ Whoohooo, I&#8217;m now officially a Zend Certified Engineer. Besides looking nice on my application it was actually a pretty good exercise and preparation for my upcoming job interviews. I wouldn&#8217;t say it was particularly hard but you definitely need to prepare yourself, if only to know what kind of questions to expect. And I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.zend.com/en/services/certification/php-5-certification/"><img src="http://www.n3rd.org/n3/images/php5-zce-logo-new.png" alt="ZCE (PHP5)" class="n3-post-img" style="float: right;"/></a> Whoohooo, I&#8217;m now officially a <a href="http://www.zend.com/en/services/certification/php-5-certification/">Zend Certified Engineer</a>. Besides looking nice on my application it was actually a pretty good exercise and preparation for my upcoming job interviews. I wouldn&#8217;t say it was particularly hard but you definitely need to prepare yourself, if only to know what kind of questions to expect. And I did learn a thing or two, some more useful than others (there is a function called <a href="http://www.php.net/manual/en/function.strspn.php"><code>strspn</code></a>? <i>Really?</i>).<br />
One could argue that quizzing people about the behaviour of particular functions isn&#8217;t really all that meaningful. After all, that&#8217;s the kind of stuff you can look up in the docs should need be. But then again, this is a PHP-specific certification and not one about your general ability to program. With that in mind it actually makes sense to ask some rather pedantic questions about PHP.</p>
<p>I&#8217;ve thought rather long and hard about what kind of jobs I should be applying for. I&#8217;ve worked with PHP, Java, C# and C++ and although PHP definitely has its quirks and shortcomings I do have a somewhat masochistic love/hate-relationship with it. It&#8217;s just a fun language. So for that reason I&#8217;ve been looking for a job as a PHP developer and it seems that there are plenty of openings and the offers are slowly trickling in. This is a very exciting time and I believe you can make or break quite a lot with your first job. Let&#8217;s see how things turn out, wish me luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.n3rd.org/2009/09/09/on-the-market/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bitter coffee With a little sugar.</title>
		<link>http://www.n3rd.org/2009/03/17/bitter-coffee-with-a-little-sugar/</link>
		<comments>http://www.n3rd.org/2009/03/17/bitter-coffee-with-a-little-sugar/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 10:31:35 +0000</pubDate>
		<dc:creator>n3rd</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.n3rd.org/?p=1181</guid>
		<description><![CDATA[Coming back to Java after working with PHP almost exclusively for several years was, not surprisingly, a very pleasant surprise. I am very excited about the upcoming features in 5.3 (closures, lambda functions, and&#8230; namespaces, finally!) and I do like PHP, but it&#8217;s most certainly no golden hammer.
One of Java&#8217;s biggest annoyances from back when [...]]]></description>
			<content:encoded><![CDATA[<p>Coming back to Java after working with PHP almost exclusively for several years was, not surprisingly, a very pleasant surprise. I am very excited about the upcoming features in 5.3 (closures, lambda functions, and&#8230; namespaces, <em>finally!</em>) and I do like PHP, but it&#8217;s most certainly no <a href="http://en.wikipedia.org/wiki/Golden_hammer">golden hammer</a>.</p>
<p>One of Java&#8217;s biggest annoyances from back when I had to deal with it in college was its lack of type variables. When using <a href="http://en.wikipedia.org/wiki/Abstract_data_type">ADTs</a> you had to do tons of potentially unsafe typecasts. And it got <em>really</em> fucking clumsy when you wanted to use primitive types in such a container because they always expected (and returned) <code>Object</code>s. Given some <code>List l</code> and <code>int i</code>, you had to weed through syntactic abscesses like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java">l.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
i <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> l.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That was about as much fun as having your intestines torn out with a hedge trimmer (and almost as messy). With J2SE 5.0 then came autoboxing and generics, and lo and behold, we can finally simply write</p>

<div class="wp_syntax"><div class="code"><pre class="java">l.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
i <span style="color: #339933;">=</span> l.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thanks to Java&#8217;s generics, gone are all those braces that painfully reminded you of the unholy days of <a href="http://en.wikipedia.org/wiki/LISP">LISP</a>. Hoo-fucking-ray.</p>
<p>So far, so good.</p>
<h2 id="toc-pairing-up">Pairing up</h2>
<p>The next thing everybody and their brother tries is to write a generic <code>Pair</code> class, by now we probably have more of those than MySpace pages *shudders*:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> A first<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> B second<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Pair<span style="color: #009900;">&#40;</span>A first, B second <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">first</span> <span style="color: #339933;">=</span> first<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">second</span> <span style="color: #339933;">=</span> second<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And suddenly you can do fancy stuff like</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java">Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span> pair <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">42</span>, <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>so you start dancing around the Playstation in your basement because while you still don&#8217;t have any friends, at least you have a generic Pair class.</p>
<p><br/></p>
<h2 id="toc-getting-equal">Getting equal</h2>
<p>Alas, this will not keep you happy for long. As soon as you try to implement the infamous <code>equals</code> you will inevitably open a can of worms big enough to fish for the next decade.</p>
<h3 id="toc-version-1">Version 1</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> other<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>other <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// from here on implicitly assumed</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>other <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// dito</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>other.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>    <span style="color: #666666; font-style: italic;">// this too</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    Pair<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> o <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span><span style="color: #009900;">&#41;</span> other<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// and this as well</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>first.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">first</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> second.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">second</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ah, blast! Pesky <code>null</code> values. Let&#8217;s try this again.</p>
<h3 id="toc-version-2">Version 2</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> other<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// the usual stuff (like above)</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> f_eq <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>first  <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> o.<span style="color: #006633;">first</span>  <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> first.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">first</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> s_eq <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>second <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> o.<span style="color: #006633;">second</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> second.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">second</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> f_eq <span style="color: #339933;">&amp;&amp;</span> s_eq<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is when you think you&#8217;re done and start hunting done some stupid, nonsensical bug and waste an entire afternoon trying to figure out what the <em>fuck</em> is going on, and since you are a Computer Science major you don&#8217;t even bother googling because what fun would that be and you&#8217;re smarter than that anyway.<br/><br />
Eventually you track it down to something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java">Pair<span style="color: #339933;">&lt;</span>X, Y<span style="color: #339933;">&gt;</span> p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>X, Y<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs TRUE</span></pre></td></tr></table></div>

<p>Crap. If everything is <code>null</code>, we don&#8217;t have jack shit to compare. But semantically it makes sense for two pairs to always be distinct if they have different type parameters, even if their members are equal. Back to the drawing board:</p>
<h3 id="toc-version-3">Version 3</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// compare against null, etc.</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #339933;">!=</span> o.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// compare individual elements</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There, <em>that</em> should do it.</p>
<p>But it doesn&#8217;t.</p>
<p>Turns out, no matter with what type parameters our generic pair is instantiated, <em>its class is always <code>Pair</code></em>. There is no way to distinguish the two, no matter what you try. And the reason for this is a nasty little thing called <a href="http://en.wikipedia.org/wiki/Java_generics#Type_erasure">type erasure</a>. After compilation the type information is simply gone, and nothing can bring it back. In other words:<br/><br />
<strong>If the individual members are equal and of the same dynamic type, then the Pair is equal because Pair&lt;A, B&gt;.class == Pair&lt;X, Y&gt;.class &#8211; </em>always</em></strong>.<br/><br />
The reason for this is that Java doesn&#8217;t use a real template mechanism. As I understand it, the type safety of generics is solely achieved during compile time. At runtime a List is a List is a List.<br/><br />
Consequently type erasure rears its ugly head even when there are no <code>null</code> values involved:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java"><span style="color: #003399;">Integer</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span> p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>i, s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Pair<span style="color: #339933;">&lt;</span>Object, String<span style="color: #339933;">&gt;</span> p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>Object, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>i, s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs TRUE</span></pre></td></tr></table></div>

<p>In a case like this you simply can&#8217;t determine that <code>p1</code> and <code>p2</code> shouldn&#8217;t be equal because they have identical types and their members have equal values.</p>
<p>So now what?</p>
<p>Turns out, we&#8217;ve one more trick up our sleeve. If the type information gets lost during compilation, how about we just keep track of it ourselves?</p>
<h3 id="toc-version-4">Version 4</h3>
<p>Let&#8217;s try this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> A first<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> B second<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Class<span style="color: #339933;">&lt;?&gt;</span> a_class<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Class<span style="color: #339933;">&lt;?&gt;</span> b_class<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Pair<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> a_c, Class<span style="color: #339933;">&lt;?&gt;</span> b_c, A fist, B second<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// assign accordingly</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> other<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>a_class <span style="color: #339933;">!=</span> other.<span style="color: #006633;">a_class</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>b_class <span style="color: #339933;">!=</span> other.<span style="color: #006633;">b_class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span> p<span style="color: #339933;">;</span>
p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>Integer, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #cc66cc;">42</span>, <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s clumsy, but it works. Problem solved! Well, almost. You can still screw yourself over if you do something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java">Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> p<span style="color: #339933;">;</span>
p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #003399;">Boolean</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And due to limitations in the language itself, you simply can&#8217;t prevent this from happening. You can, however, make it a tad but more foolproof by putting type constraints in the constructor:</p>
<h3 id="toc-version-5">Version 5</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> Pair<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> A<span style="color: #339933;">&gt;</span> a_c, Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> B<span style="color: #339933;">&gt;</span> b_c, A first, B second<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Again, this still isn&#8217;t the holy grail. But it does enforce some kind of inheritance between <code>A</code> and <code>a_c</code>. So you can&#8217;t accidentally write something like <code>new Pair(Integer.class, String.class, false, "foo")</code>. You could just as well use <code>extend</code> instead of <code>super</code>, but then you lose the minor benefit of the compiler keeping you from putting the parent class on the left hand side of the assignment which is what you do all the time (think <code>List l = new LinkedList()</code> and such).</p>
<h2 id="toc-conclusion">Conclusion</h2>
<p>Java Generics were a huge step forward, and they are undoubtedly &#8220;good enough&#8221; for most practical purposes. You will, however, occasionally encounter situations where you long for the raw template power of languages like C++.<br />
Granted, the approach I outlined seems a bit unnatural and is, in a way, error prone since it forces you to enter redundant data that will blow up in your face if it is inconsistent. But if you want the added semantics described above, this is about the best you can do in Java.<br/></p>
<p class="n3-ps">P.S.: I am not sure how far you would get using Reflection. In any case it isn&#8217;t something you should be doing in a low-profile all-purpose ADT. If you really wanna read how deep the rabbit hole goes, take a look at <a href="http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html">this</a>.</p>
<hr class="n3-movie-roundup-split"/>
<h3 id="toc-my-pair-skeleton">My <code>Pair</code> Skeleton</h3>
<p>Here&#8217;s a slimmed-down version of my <code>Pair</code> class that you can use for your own implementation, should you be interested in it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pair<span style="color: #339933;">&lt;</span>X, Y<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;?&gt;</span> cx<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;?&gt;</span> cy<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> X x<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Y y<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Pair<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> X<span style="color: #339933;">&gt;</span> cx, Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> Y<span style="color: #339933;">&gt;</span> cy, X x, Y y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cx</span> <span style="color: #339933;">=</span> cx<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cy</span> <span style="color: #339933;">=</span> cy<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>cx <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>cy <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>x.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> cx<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>y <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>y.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> cy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// map two hashcodes to one</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> x.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>y <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> y.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> o<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>o <span style="color: #000000; font-weight: bold;">instanceof</span> Pair<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        Pair<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span> other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;?</span>, <span style="color: #339933;">?&gt;</span><span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>cx.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>other.<span style="color: #006633;">cx</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>cy.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>other.<span style="color: #006633;">cy</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">boolean</span> eqx <span style="color: #339933;">=</span> x <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> other.<span style="color: #006633;">x</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> x.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>other.<span style="color: #006633;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> eqy <span style="color: #339933;">=</span> y <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> other.<span style="color: #006633;">y</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> y.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>other.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> eqx <span style="color: #339933;">&amp;&amp;</span> eqy<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(%s,%s)&quot;</span>, x, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.n3rd.org/2009/03/17/bitter-coffee-with-a-little-sugar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I saw assign&#8230;</title>
		<link>http://www.n3rd.org/2009/03/05/i-saw-assign/</link>
		<comments>http://www.n3rd.org/2009/03/05/i-saw-assign/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 15:14:41 +0000</pubDate>
		<dc:creator>n3rd</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.n3rd.org/?p=1151</guid>
		<description><![CDATA[..and it fucked up my code, I saw assign. Ten points for anyone who gets the pop reference, and an extra five for those who get the pun.
Take a peek at this snippet of JavaScript code and tell me why it bails out in line 7 with a nasty &#8220;foo.bar is not a function&#8221; type [...]]]></description>
			<content:encoded><![CDATA[<p><em>..and it fucked up my code, I saw assign.</em> Ten points for anyone who gets the pop reference, and an extra five for those who get the pun.</p>
<p>Take a peek at this snippet of JavaScript code and tell me why it bails out in line 7 with a nasty &#8220;<code>foo.bar is not a function</code>&#8221; type error despite the explicit check against <code>null</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	isBar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// do stuff with foo</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>foo <span style="color: #339933;">=!</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> foo.<span style="color: #006600;">isBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course, stripped down to less than ten lines and with all the confusing stuff like callbacks, inheritance or asynchronous function calls removed, the error is blatantly obvious to see: it&#8217;s a simple typo. Instead of &#8220;<code>foo != null</code>&#8221; I wrote &#8220;<code>foo =! null</code>&#8221; and that is equivalent to &#8220;<code>foo = !null</code>&#8220;, which is an <em>assignment</em> and effectively throws <code>foo</code> away and replaces it with the value of <code>!null</code> (which happens to be <code>true</code>). And, sure enough, <code>true.bar()</code> doesn&#8217;t really work all that well. Classic case of <a href="http://en.wikipedia.org/wiki/PEBKAC">PEBCAK</a>.</p>
<p>The irony in this is that just a couple of days ago I was talking to my roomie about how some people consider it good style to do boolean expressions with variables &#8220;backwards&#8221; like such:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span> <span style="color: #339933;">==</span> <span style="color: #000033;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The argument being that it prevents errors like the one shown above since &#8220;<code>500 = $x</code>&#8221; blows up in your face right away with a syntax error. We basically agreed that it looks stupid and is, in essence, retarded.</p>
<p>Fifteen minutes of tedious debugging and an considerable amount of profanity later I am starting to think maybe those guys were onto something after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.n3rd.org/2009/03/05/i-saw-assign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
