<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ismael Juma &#187; jatran</title>
	<atom:link href="http://blog.juma.me.uk/tag/jatran/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.juma.me.uk</link>
	<description></description>
	<lastBuildDate>Fri, 18 Nov 2011 00:09:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.juma.me.uk' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ismael Juma &#187; jatran</title>
		<link>http://blog.juma.me.uk</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.juma.me.uk/osd.xml" title="Ismael Juma" />
	<atom:link rel='hub' href='http://blog.juma.me.uk/?pushpress=hub'/>
		<item>
		<title>Array variance and method overloading</title>
		<link>http://blog.juma.me.uk/2008/09/21/array-variance-and-method-overloading/</link>
		<comments>http://blog.juma.me.uk/2008/09/21/array-variance-and-method-overloading/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 04:34:09 +0000</pubDate>
		<dc:creator>Ismael Juma</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[array variance]]></category>
		<category><![CDATA[jatran]]></category>
		<category><![CDATA[java to scala translation]]></category>
		<category><![CDATA[method overloading]]></category>

		<guid isPermaLink="false">http://ijuma.wordpress.com/?p=16</guid>
		<description><![CDATA[One of these days I used jatran to translate the code for a simple Swing application from Java to Scala. Jatran&#8217;s translation had a few issues (some of which have been fixed since I tried it) so there were quite a few compiler errors after it finished. I fixed them and launched the application. It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.juma.me.uk&amp;blog=4860094&amp;post=16&amp;subd=ijuma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of these days I used <a href="http://code.google.com/p/jatran/">jatran</a> to translate the code for a simple Swing application from Java to Scala. Jatran&#8217;s translation had a few issues (some of which have been fixed since I tried it) so there were quite a few compiler errors after it finished. I fixed them and launched the application.</p>
<p>It was at this point that I noticed a change in behaviour and it was not clear why it was happening. After some investigation, it turned out that it was caused by the fact that variance of arrays in Scala is different from Java combined with poor usage of method overloading.</p>
<p>TreePath has two constructors that do different things, but one of them takes a supertype of the other:</p>
<p><pre class="brush: java;">
public TreePath(Object[] path)
</pre></p>
<p><pre class="brush: java;">
public TreePath(Object singlePath)
</pre></p>
<p>Because arrays are nonvariant in Scala, it means that scalac chooses the constructor that takes an Object for the following code:</p>
<p><pre class="brush: java;">
def pathToRoot: Array[TreeNode] = ...
val treePath = new TreePath(pathToRoot)
</pre></p>
<p>On the other hand, arrays are covariant in Java so the constructor that takes an Object[] is chosen by javac for the equivalent Java code. One way to force scalac to choose the correct constructor is to cast the result of pathToRoot:</p>
<p><pre class="brush: java;">
def pathToRoot: Array[TreeNode] = ...
val treePath = new TreePath(pathToRoot.asInstanceOf[Array[AnyRef]])
</pre></p>
<p>It&#8217;s a subtle issue that may affect other people automatically converting Java code to Scala, so I thought I&#8217;d bring it up in case it saves them some time. :)</p>
<br />Posted in scala Tagged: array variance, jatran, java to scala translation, method overloading, scala <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ijuma.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ijuma.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ijuma.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.juma.me.uk&amp;blog=4860094&amp;post=16&amp;subd=ijuma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.juma.me.uk/2008/09/21/array-variance-and-method-overloading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0eb053bf15b0a3669c973bc50be45a7c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ijuma</media:title>
		</media:content>
	</item>
	</channel>
</rss>
