Sunday, January 17, 2010

Learning Scala pt 1: First Impressions

My first foray into learning Scala with the help of O'Reilly's Learning Scala, and the interwebs. I'll be posting things as I learn them in a way to help cement my understanding of the language and so if I'm wrong, people can call me on it.

As a Java programmer, I've been very interested in watching all the languages that are beginning to spring up around the JVM. From JRuby, Jython, Groovy, Clojure, and Scala they've all been interesting to watch take the JVM to new heights beyond Java. So far, I've been mostly interested in languages started on the JVM (though JRuby and Jython look nice) since they are designed from the ground up to take advantage of the vast number of Java libraries in a natural way.

My first dabble was with Groovy which takes many of its ideas from Ruby, but can still be written in a very Java-centric way. Specifically, Groovy was written to be more of a superset of Java including duck typing and Closures into the mix and letting you write applications much more quickly and concisely than one could in Java. Unfortunately I haven't really played with Groovy much recently but it seems like it would be a great JVM-based glue language or for writing up a quick prototype.

Scala has been drawing me in over the past couple weeks and feels like it could be a full replacement language for Java where with Groovy it felt like an additional tool in my Java workbench. After reading Chapter 1 of Learning Scala here are my impressions of Scala thus far:

Functional and Object Oriented

From the start Scala mixes both functional aspects (much of which still confuse my Object Oriented mind so far), and Object Oriented programming. We have Classes and inheritance but also have pattern matching and Actors.

Less Verbose than Java

Scala, like many other languages, is less verbose than Java in several ways. First, there is no need for the semicolon (in most cases), or even for return statements of methods. Scala will usually handle these things for you, so a method foo, that adds two numbers, could be written like this:
def foo = { 1 + 1 }
Which returns an Integer.

A nice feature of Scala is that you can choose to specifically type your variables or let Scala figure that out. In Scala, the type comes after the variable name to make it easy to parse when the variable is not typed.

Strange at First

As a Java programmer I've gotten pretty used to how things are with Java so beginning to learn Scala has left a few parts of my mind that stick out.

Method Names

Scala is much more flexible in what a method name can be, this includes these characters to name a few: "+,-,<,>,*,/,?,!". This seemed very unnatural to me coming from Java where this is not possible. This also is confusing because it looks like operator overloading but actually isn't operator overloading, these are method names, so one could define a "+" method or a ">" method and be perfectly fine with Scala.

This will definitely take some getting used to but I think it could be very handy especially when the need for a DSL arises.

No static keyword

With Java, declaring a class-level variable is as easy as tacking the static keyword to it. With Scala, there is no static keyword, instead, for class level variables you use the object keyword instead of class.
class foo() {         
    // Instance variables go here 

object foo() {         
    // Class level variables go here

Overall Scala isn't too different from Java on first glance. While there certainly will be more differences, it is easy enough for me to start jumping in and playing around with Scala.

No comments:

Post a Comment