<?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>Daniel Pitts' Tech Blog &#187; smoots</title>
	<atom:link href="http://virtualinfinity.net/wordpress/tag/smoots/feed/" rel="self" type="application/rss+xml" />
	<link>http://virtualinfinity.net/wordpress</link>
	<description>Daniel Pitts' Tech Blog</description>
	<lastBuildDate>Thu, 04 Feb 2010 19:10:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>More Discussion On Operator Overloading</title>
		<link>http://virtualinfinity.net/wordpress/java/2007/12/05/more-on-operator-overloading/</link>
		<comments>http://virtualinfinity.net/wordpress/java/2007/12/05/more-on-operator-overloading/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 19:46:38 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[avoiding abuse]]></category>
		<category><![CDATA[interfaces]]></category>
		<category><![CDATA[operator overloading]]></category>
		<category><![CDATA[smoots]]></category>

		<guid isPermaLink="false">http://virtualinfinity.net/wordpress/java/2007/12/05/more-on-operator-overloading/</guid>
		<description><![CDATA[Updated: See notes below. I was surprised to see that within one day of posting my previous entry on Operator Overloading, I received several comments. Aviad Ben Dov from Chaotic Java even took my idea and ran with it. Ricky Clarkson suggested using Haskell&#8217;s approach of allowing anything that is of the &#8220;Num&#8221; type to [...]]]></description>
			<content:encoded><![CDATA[<p><em>Updated: See notes below.</em></p>
<p>I was surprised to see that within one day of posting my previous entry on <a href="http://virtualinfinity.net/wordpress/almost-useful/2007/12/04/almost-useful-operator-overloading/" title="Almost Useful: Operator Overloading">Operator Overloading</a>, I received several comments.  Aviad Ben Dov from <a href="http://chaoticjava.com/" title="Chaotic Java">Chaotic Java</a> even took my idea and <a href="http://chaoticjava.com/posts/who-wants-indexer-as-the-next-eod-feature/" title="Chaotic Java » Blog Archive » Who wants Indexer as the next EoD feature?">ran with it</a>.  <a href="http://cime.net/~ricky/" title="Ricky Clarkson's Bits and Pieces">Ricky Clarkson</a> suggested using Haskell&#8217;s approach of allowing anything that is of the &#8220;Num&#8221; type to define +,-,/,*, etc&#8230;. I have a few things to add to this discussion.</p>
<p>Aviad&#8217;s idea for operators by interface is not a bad one; it works well for overloading &#8220;[]&#8221; but it breaks down on a few use cases (such as &#8216;+&#8217;, &#8216;*&#8217;, etc..) that are important (to me).  Ricky&#8217;s idea for subtypes of a specific class getting to have operator overloading isn&#8217;t bad either, but for physical unit manipulation it is too inflexible.  The core concept that both of them seem to have suggest is that a limited selection of types can have overloaded operators, but the operations that are possible aren&#8217;t limited to the scalar quantities that this would limit the operators to.</p>
<p>Suppose I have the classes Distance, Area, and the built-in &#8220;Scalar&#8221; type Double I would expect at least these sets of operations:<br />
Distance * Distance =&gt; Area<br />
Distance * Double =&gt; Distance</p>
<p>If I had to implement the <code>Multipliable&lt;T&gt;</code>  interface, I wouldn&#8217;t be able to handle Distance * Distance <em>and</em> Distance * Double. You can&#8217;t implement an interface twice, even with different type parameters.  I don&#8217;t know if this is something that Reified generics would fix, but it feels like it might be.  Maybe someone could comment on that.</p>
<p>Also, if Distance had to extend Number, what would doubleValue return? Meters? Inches? <a href="http://en.wikipedia.org/wiki/Smoot" title="Smoot - Wikipedia">Smoots</a>?  There might be some way to solve these problems, but I can&#8217;t think of a way to prevent abuse while allow good use.</p>
<p>Actually, now that I have thought a little about it&#8230;</p>
<p>The semantics of plus (+), minus (-), times (*), dividedBy (/), moduloOf (%),  shiftLeft(&lt;&lt;), shiftRight(&gt;&gt;), unsignedShiftRight(&gt;&gt;&gt;), or(|), and(&amp;), xor (^), negative(-), and inverse(~), are all well-defined enough for so many not-necessarily-numeric types that allowing, even if only through naming conventions, the overloading of those operations seems like a good idea.</p>
<p>I think a good way to go would be to convert at compile time <code>a * b</code> to the method call <code>a.times(b)</code>. Assignment operators like <code>a += b</code> would be replaced with <code>a = a.plus(b)</code>. This would help reduce abuse while creating a more expressive language.  The assignment operator rule is important, as it will help prevent the &#8220;<em>clever</em>&#8221; idiom of using += for appending elements to a collection.</p>
<p><em>Note on updates: I previously misspelled Aviad as &#8220;Avaid&#8221;.  I also have added clarification for which use-cases Aviad&#8217;s Indexer doesn&#8217;t work for me, namely for algebraic operators.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://virtualinfinity.net/wordpress/java/2007/12/05/more-on-operator-overloading/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
