Tricky Things in Scala-land

I’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’ve turned that document into this blog post. A few things only apply to Scala 2.7, but thems the breaks.

  • Inner classes (as in the Foo class has an inner Bar class) are accessed as Foo#Bar. You can avoid writing those every with a type MyBar = Foo#Bar in the class or object that’s using the inner class.

  • Getting the class while in the class definition portion of your code is not via Foo.class but classOf[Foo].

  • <%, 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 “only allow types that can be turned into this another type”. For instance, I once wanted a min method 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 <%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 Iterable.min in Scala 2.7. In Scala 2.8, min is nicely defined on types that are Iterables. Also, please ignore how terrible that one-liner min method is.

  • Oh, yeah, by the way, Iterable.min exists in Scala 2.7. Would have saved me learning more about the Scala type system than I really cared to at that point.

  • If you get an “integer too large” when putting in a raw number (like when trying val l = 0xc6a4a7935bd1e995?), you forgot the ‘L’. As in, val l = 0xc6a4a7935bd1e995L. This is hopefully the dumbest thing I had to figure out on this list.

  • To define a method or constructor that takes a subclass as it’s argument, you have to use a view bound, <%, to ensure the type of the class. Say, for instance, you want to construct a subclass of Hadoop’s ArrayWritable which requires a class to be passed to it that is a subclass of Writable. To do that you would write:

  • 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’ll have to use the type trick and a shim class to work with it properly. For instance, Hadoop’s Mapper class required me to do this:

  • The default constuctors syntax is odd. The error message (“error: wrong number of arguments for constructor”) 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’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 extends line. So, given the code below, the important part is to pass bar in to Mine as Mine(bar) in that extends line.

  • Here’s a small list of methods that are useful when trying to explore your way around a library in the Scala REPL.

  • 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’t overriding a method in its superclass.

  • Also, when using javap, don’t forget that Scala objects have a $ added to the end of their name, so double check the names of the actual class files.

  • When using javap, your shell will probably require the $ to be escaped (as \$) or have single quotes around it to parse your input properly. e.g. javap -c 'Outer$Inner'.

  • Oh, and adding -private and -verbose to your javap arguments is how you find out anything really useful.

Mid-twenties

Congratulations! Welcome to your mid-twenties!

You now believe that your greatness is only limited by your own flawed being. For the next decade, your estimate of self-worth will oscillate between fraud and failure.

These feelings will continue until you realize that they are one of the common events of being a human being with the majority of its needs cared for. This acceptance will come slowly and be a relief from an omnipresent stress you stopped noticing you felt.

This acceptance will be difficult for you to distinguish from simply settling for less and leaving your talents to fallow.

Good luck!

The First Commitment

In the past few months, I’ve allowed myself to slip. I haven’t been making many public commits, nor discussing much where others can see. It has me feeling like a bodybuilder who hasn’t touched a set of weights in the same amount of time. My work and my writing has atrophied. My ability to maintain code that other people depend upon has suffered, and my ego has, as well. Time to sharpen up.

The first part of this new commitment is that I’ll be making a minimum of 3 commits a week to rFeedParser, no matter how small. This one is a stepping stone to taking on more of a workout, and It gives me time to reacquaint myself with the code base. rFP has weird and hairy parts in it because the problem it was solving was weird and hairy. However, there are a good number of ugly parts that were created because a) I wrote it with the Python version in the next window over causing me to write with a strong Pythonic accent; and b) I wasn’t as skilled in Ruby as I am now.

The module hierarchy alone proves I was diving in and not giving a fuck. At a certain point, I was just trying to get it to goddamn work and not caring what kind of hack-and-slash maneuvers I had to pull off to make it happen. With the distance from the problem and the clearer head I have now, I can piece together how it should be done.

The second part is a commitment to one commit a week to one of my public side projects. Right now, this consists mainly of the strictly-for-fun-and-I’m-keeping-it-that-way-fuckers framework I’m writing called Recess. Everyone writes a web framework, and I’m going to be That Guy, too.

I’ll try not to be too snooty about it, but if the framework turns out well (or, at all, really), I probably will be. Like I’ve said before, my ego knows no bounds. But, remember! It’s just for fun. Really. Really.

As my plans and projects grow and adapt and interests wax and wane, there will, of course, be a call to change this commitment. This two-part commitment is only the first of what will be a series of changing, and, likely, growing vows to myself. Look to see a lot more work from me.


Archive