<?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>David R. MacIver &#187; jvm</title>
	<atom:link href="http://www.drmaciver.com/tag/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.drmaciver.com</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 11:12:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A curious fact about overloading in Scala</title>
		<link>http://www.drmaciver.com/2008/08/a-curious-fact-about-overloading-in-scala/</link>
		<comments>http://www.drmaciver.com/2008/08/a-curious-fact-about-overloading-in-scala/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 21:59:54 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.drmaciver.com/?p=219</guid>
		<description><![CDATA[So I was taking a look at the Scala Array source code, as part of research for a forthcoming blog post (coming soon to a drmaciver.com near you) when I suddenly realised something. Then I suddenly had a WTF moment. The scala.Array object has a method appy(xs : Int*) : Array[Int], and a method appy(xs [...]]]></description>
			<content:encoded><![CDATA[<p>So I was taking a look at the Scala Array source code, as part of research for a forthcoming blog post (coming soon to a drmaciver.com near you) when I suddenly realised something. Then I suddenly had a WTF moment.</p>
<p>The scala.Array object has a method appy(xs : Int*) : Array[Int], and a method appy(xs : Double*) : Array[Double], and&#8230; etc. i.e. it has an overload for each value type and one for the reference types.</p>
<p>Something like this:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Overload<span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#40;</span>xs <span style="color: #000080;">:</span> String<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;foo&quot;</span><span style="color: #000080;">;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#40;</span>xs <span style="color: #000080;">:</span> Int<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;bar&quot;</span><span style="color: #000080;">;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p></code></p>
<p>Um.</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">/home/david/Foo.<span style="color: #000000;">scala</span><span style="color: #000080;">:</span><span style="color: #F78811;">3</span><span style="color: #000080;">:</span> error<span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">double</span> definition<span style="color: #000080;">:</span>
method foo<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>Int<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> and
method foo<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span> at line <span style="color: #F78811;">2</span>
have same <span style="color: #0000ff; font-weight: bold;">type</span> after erasure<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Seq<span style="color: #F78811;">&#41;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#40;</span>xs <span style="color: #000080;">:</span> Int<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;bar&quot;</span><span style="color: #000080;">;</span>
      ^</pre></div></div>

<p></code></p>
<p>See, Scala varargs use Seq, so all varargs erase to the same thing.</p>
<p>So how the hell is scala.Array working? Is it compiler magic?</p>
<p>Well, sortof compiler magic. It&#8217;s a compiler magic you too can use. Lets change the above code slightly:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Overload<span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#40;</span>xs <span style="color: #000080;">:</span> String<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;foo&quot;</span><span style="color: #000080;">;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> foo<span style="color: #F78811;">&#40;</span>xs <span style="color: #000080;">:</span> Int<span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span><span style="color: #000080;">;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p></code></p>
<p>This compiles and works fine.</p>
<p>So, what&#8217;s going on here?</p>
<p>Well, although this fact is visible only as an implementation detail in most languages, the JVM actually lets you overload based on return type. Two methods are considered identical if they have precisely the same (erased) argument and return types. And although Scala doesn&#8217;t let you overload based on return type directly, it seems it will happily accept methods whose arguments erase to the same thing as long as their return types don&#8217;t.</p>
<p>This is a bit strange. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.drmaciver.com/2008/08/a-curious-fact-about-overloading-in-scala/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

