Saturday, June 13, 2009

Scala Review: Quite Refreshing

Since my first programming course in college, I've been told to use the right programming tool for the job. This was back in the day where perl was the ideal CGI and scripting language. I think even without python, perl was still the wrong tool for the job.

Regardless, I have used java many times for testing any idea floating around in my head. It had the relative speed of C++ and came with many more libraries than C++ making it a lot easier to perform some arbitrary task like load an image without having to find a special C++ library like libpng.

It's hard being number 2
Java sure has its faults. It certainly isn't the sexy language it was ten years ago, but it sometimes gets flak it really doesn't deserve. It is always criticized for being slower than C++, while no one mentions it being faster than most other languages. I guess it's hard being the number 2 in performance.

Java is a relatively simple language in the sense that it's pretty easy to understand what the code is doing. C++ trivia questions abound that make you ask yourself, "Why?! Why?! Why?! That must be a compiler bug!" until you see it in a C++ specification. Java dropped function passing, pointers, multiple inheritance, operating overloading, and a lot of other features to create a language one might call dumbed down.

This, in my opinion, is one of Java's main complaints. It's a simple language. Usually when someone will start listing examples, it's them lamenting that Java can't do it their way like designing classes with multiple inheritance--sure, it means you have to rethink your problem, or you may have to type a bit more for a given problem, but I think deep down the person just wants to be different.

Scala building off of Java's success
For many tasks, Java is everything I need it to be

  1. The built-in packaging is much better than many other languages. Those who refer to it as "jar hell" are just bitter C++ developers
  2. It is a relatively fast language. Java is usually not at the top on many language performance benchmarks, but it often comes as a close second.
  3. Java comes with some great libraries. Some have gone as far as to criticize Java for having too many libraries, but these are just bitter C++ developers. It's not like one library is obscuring another by popularity. If I need java networking, an image library isn't going to throw me off the trail to find it
  4. It has tons of examples. I can't even count how many times I want to do some mundane task and a google takes me to an EXACT example in the java almanac

Now, as much as I like Java, it will never displace all the other tools in my toolbox. It's just a really handy tool I'm glad to have there. Yet despite all the bonuses of Java as a language, there were always some features I didn't need or know I wanted, but when I found Scala, it literally opened my eyes. Well, maybe not literally...

Adding some nice features
It's hard to summarize from a global perspective why I enjoy Scala, so here are a few small features that stood out to me.

  • Scala compiles to java byte code, so it has access to all java libraries ever written. Java code can even use compiled scala code
  • In many tests I've seen, it is comparatively fast to java
  • Scala allows function passing, but in a better way than python. An entire function block can be written inside a function call, which is a lot more powerful than python's lambda
  • Fun match ability, which many refer to as a switch statement on steroids. I recently learned it is an easy way to do type checking
  • Syntax-supported singleton objects, lazy evaluation of members, ...
  • The package system syntax is more flexible than java, where multiple public classes can be in the same file. I believe even several classes from different packages can be stored in the same file
  • Tuples! I never appreciated tuples until I started working in python. Going back to java and having to write a separate class just to return two or three values is incredibly frustrating. Java wants them; Scala has them
  • Scala is strongly-typed. Some people may loathe this, but I actually prefer it. When writing difficult code, I have never met a developer whose bottleneck is having to decide what type a variable should be. It's writing code the compiler can heavily optimize, which is easier in a strongly-typed language and shows a lot of the problems at compile time instead of run time

A note on syntax
One of the Scala complaints I feel I must address is the syntax. Some people would have preferred Scala to have a C-like syntax like so many other popular languages. Now I myself also look at a language and sometimes cringe at the lack of curly-brace-separated blocks or not passing functions with parenthesis, but Scala is actually very simple to get used to. Here are a few little differences that come to my head.

object SingletonThing {
def display(name:String) {
println("I'm ready to work")
println("Thank you for calling me, " + name)

Here's a singleton object. It's very similar to the static methods one would put inside a class in java. To make a singleton, just use the object keyword instead of class. Also functions begin with the keyword def. I was adamantly resistant to this syntax for about ten minutes. Also, parameter strings reverse the C++ order of types and variables. I actually prefer this order now.

class GiveMeAFunction(someNumber:int) {
println("This is part of the GiveMeAFunction constructor")
val anotherNumber = someNumber + 5

def doSomethingWithTheNumbers(someFunctionYouGiveMe:(int,int) => Unit) {
someFunctionYouGiveMe(someNumber, anotherNumber)

This example gives a few more insights into scala basics. First, scala classes are declared with a default constructor parameter string and its constructor code trickles into the body. It is possible to have secondary constructors, but I haven't needed them yet.

Second, you'll notice the val keyword with member declared. The two main ways to store a reference is with val meaning that that value will remain constant, and var meaning it is a variable. Using val supposedly allows the compiler to make certain optimizations, but it is more useful as a quick way to not accidentally mess up the value you have stored in it. This syntax is somewhere in between C++ and python. You can declare a value as variable or constant like C++, yet you don't have to manually retype the type similar to python. I consider Scala's implementation superior to python's as once you declare a variable, it will remain that type where python will let you put whatever you want into it at any time. This has been a headache for me when coding in python and I'll accidentally put a function into a variable instead the function's return value, which it won't complain about until somewhere later in execution.

And lastly is the function passing. I created a method which takes a function someFunctionYouGiveMe, which has to take two integers as parameters with Unit (means void) return type. I can then call that function or save it off to be used later. I have grown to love this syntax.

Other than that, there are only minor differences to Java. if statements must be on the same line as their code if one doesn't used a code block. Semi-colons aren't needed unless you really want to put two commands on the same line. Also, importing is a little more robust than Java.

Scala is what Java should have been
I encourage any person who enjoys Java or wants a little experience to toy around in Scala. It's a functional/object-oriented hybrid that has been a really fun transition from Java.

A question I might be asked is if Scala is really so cool, why would I ever use Java again? Well, the only reason I use Java now is to take advantage of Netbean's GUI-building forms. Other than that, I will probably use Scala for everything else. Some may consider Scala a fad, but I think with it's added features and friendly play with the Java language, it will be around as long as the JVM will be.