<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Something Similar</title>
 <link href="http://somethingsimilar.com.com/atom.xml" rel="self"/>
 <link href="http://somethingsimilar.com/"/>
 <updated>2011-08-22T11:33:27-07:00</updated>
 <id>http://somethingsimilar.com/</id>
 <author>
   <name>Jeff Hodges</name>
   <email>jeff@somethingsimilar.com</email>
 </author>

 
 <entry>
   <title>Tricky Things in Scala-land</title>
   <link href="http://somethingsimilar.com/2011/01/13/tricky-things-in-scala/"/>
   <updated>2011-01-13T12:00:00-08:00</updated>
   <id>http://somethingsimilar.com/2011/01/13/tricky-things-in-scala/</id>
   <content type="html">&lt;p&gt;I&amp;#8217;ve got an old document here describing some of the tricky things and gotchas I found while learning Scala (and some things I had to be reminded of about the JVM) and trying to build on top of Hadoop. I&amp;#8217;ve turned that document into this blog post. A few things only apply to Scala 2.7, but thems the breaks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Inner classes (as in the &lt;code&gt;Foo&lt;/code&gt; class has an inner &lt;code&gt;Bar&lt;/code&gt; class) are accessed as &lt;code&gt;Foo#Bar&lt;/code&gt;. You can avoid writing those every with a &lt;code&gt;type MyBar = Foo#Bar&lt;/code&gt; in the class or object that&amp;#8217;s using the inner class.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Getting the class while in the class definition portion of your code is not via &lt;code&gt;Foo.class&lt;/code&gt; but &lt;code&gt;classOf[Foo]&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;%&lt;/code&gt;, the view bound operator, lets you accept a container with objects inside that may obey a another trait. The not-exactly-correct meaning of its use is &amp;#8220;only allow types that can be turned into this another type&amp;#8221;. For instance, I once wanted a &lt;a href='https://gist.github.com/109129'&gt;&lt;code&gt;min&lt;/code&gt; method&lt;/a&gt; to implicitly exist on Array. For the method to compile, it had to only accept Arrays that contained types that could be wrapped in Orderable. Using &lt;code&gt;&amp;lt;%&lt;/code&gt;in the type parameter of the method made this possible. Of course, this turned out to be a moot point when I found the static (that is, object) method &lt;code&gt;Iterable.min&lt;/code&gt; in Scala 2.7. In Scala 2.8, &lt;code&gt;min&lt;/code&gt; is nicely defined on types that are Iterables. Also, please ignore how terrible that &lt;a href='https://gist.github.com/109129'&gt;one-liner &lt;code&gt;min&lt;/code&gt; method&lt;/a&gt; is.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Oh, yeah, by the way, &lt;code&gt;Iterable.min&lt;/code&gt; exists in Scala 2.7. Would have saved me learning more about the Scala type system than I really cared to at that point.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;If you get an &amp;#8220;integer too large&amp;#8221; when putting in a raw number (like when trying &lt;code&gt;val l = 0xc6a4a7935bd1e995&lt;/code&gt;?), you forgot the &amp;#8216;L&amp;#8217;. As in, &lt;code&gt;val l = 0xc6a4a7935bd1e995L&lt;/code&gt;. This is hopefully the dumbest thing I had to figure out on this list.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;To define a method or constructor that takes a subclass as it&amp;#8217;s argument, you have to use a view bound, &lt;code&gt;&amp;lt;%&lt;/code&gt;, to ensure the type of the class. Say, for instance, you want to construct a subclass of Hadoop&amp;#8217;s &lt;code&gt;ArrayWritable&lt;/code&gt; which requires a class to be passed to it that is a subclass of &lt;code&gt;Writable&lt;/code&gt;. To do that you would write:&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Because of the difference between how Scala and Java look up inner classes, when attempting to work with a Java classes that passes its type parameters to its inner classes, you&amp;#8217;ll have to use the &lt;code&gt;type&lt;/code&gt; trick and a shim class to work with it properly. For instance, Hadoop&amp;#8217;s Mapper class required me to do this:&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;The default constuctors syntax is odd. The error message (&amp;#8220;error: wrong number of arguments for constructor&amp;#8221;) is clearer to to someone who knows Scala because of the little arrow pointing at the supertype in question but it caused me some consternation and javadoc hunting before realizing, no, I wasn&amp;#8217;t wrong about the type of my superclass, and, instead, just forgot the syntax for default constructors. You just have to pass your constructor args to the superclass in the &lt;code&gt;extends&lt;/code&gt; line. So, given the code below, the important part is to pass &lt;code&gt;bar&lt;/code&gt; in to &lt;code&gt;Mine&lt;/code&gt; as &lt;code&gt;Mine(bar)&lt;/code&gt; in that &lt;code&gt;extends&lt;/code&gt; line.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Here&amp;#8217;s a small list of methods that are useful when trying to explore your way around a library in the Scala REPL.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Use javap to figure out what methods are actually on an instance of your class. This is often useful to figure out why your method isn&amp;#8217;t overriding a method in its superclass.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Also, when using javap, don&amp;#8217;t forget that Scala &lt;code&gt;object&lt;/code&gt;s have a &lt;code&gt;$&lt;/code&gt; added to the end of their name, so double check the names of the actual &lt;code&gt;class&lt;/code&gt; files.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;When using &lt;code&gt;javap&lt;/code&gt;, your shell will probably require the &lt;code&gt;$&lt;/code&gt; to be escaped (as &lt;code&gt;\$&lt;/code&gt;) or have single quotes around it to parse your input properly. e.g. &lt;code&gt;javap -c &amp;#39;Outer$Inner&amp;#39;&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Oh, and adding &lt;code&gt;-private&lt;/code&gt; and &lt;code&gt;-verbose&lt;/code&gt; to your javap arguments is how you find out anything really useful.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 
</feed>

