Friday, March 24, 2006

Scale up with Scala

Of all the debates raging the programming languages space of the web, many a blood has been shed on the static versus dynamic typing issues. Statically typed languages running on a virtual machine are busy incorporating dynamic typing in various forms - Java has officialized JSR 292 (I blogged on this before), while C# has been aggressive in supporting dynamic language features within the CLR. Besides, dynamically typed languages like Ruby, Groovy, Python etc. have been seeking their destiny in all available virtual machines. Amongst all this brouahha, one new language has been able to carve out a niche in the programming language fraternity by its solid design and elegance of style. Of course I am talking about Scala, the new kid on the block, a masterpiece from Martin Odersky, which can run on the JVM and can cross-compile into a .NET assembly.

In today's world of complex enterprise applications, objects alone are not able to deliver all the goods, multiparadigm design forces compel one to think in terms of artifacts which provide higher degrees of abstraction over the problem domain. As an example, consider the world of Web Services along with its rich XML manipulation requirements, which make us yarn for more powerful tree transformers, not offered by current OO languages. Think functional - this is what the document on Scala Rationale hints at! Enter Scala - the language designed with the mission of unifying object-oriented and functional elements. Martin Odersky sums it up beautifully in the document [Programming In Scala] :
Scala is both an object-oriented and functional language. It is a pure objectoriented
language in the sense that every value is an object. Types and behavior of objects are described by classes. Classes can be composed using mixin composition. Scala is designed to work seamlessly with mainstream object-oriented languages, in particular Java and C#.
Scala is also a functional language in the sense that every function is a value. Nesting of function definitions and higher-order functions are naturally supported. Scala also supports a general notion of pattern matching which can model the algebraic types used in many functional languages. Furthermore, this notion of pattern matching naturally extends to the processing of XML data.

I plan to blog a lot on Scala, focusing on all niceties, which have the potential to position it as the language of the future. Apart from the fact that Scala supports OO as well as functional paradigm, one other sublime aspect of Scala is its extensibility - Scala is a programmable programming language.

No introduction to a programming language is complete without the HelloWorld program. I would like to end today's post precisely with that. The following is the Scala way of saying Hello, World!

object HelloWorld {
    def main(args: Array[String]): unit = {
        System.out.println("Hello, world!");
    }
}

Trivial as it may seem, the above snippet brings out some very interesting thoughts behind the design of Scala. Let us compare it with the corresponding version in Java.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

In the Java version we use the class based encapsulation, while Scala introduces the object declaration for the main method. This declaration introduces a Singleton object along with the class named HelloWorld, which is created on demand, the first time it is used. The HelloWorld program makes the Singleton Design Pattern "invisible" in Scala - that's really hell of a HelloWorld!

When we can construct singletons invisibly, why should we require statics ? That's the second point of note
static members (methods or fields) do not exist in Scala. Rather than defining static members, the Scala programmer declares these members in singleton objects.

Coming Up! Everything is an Object in Scala!

3 comments:

Anonymous said...

Funny, I've been looking at Scala for the last week or so myself.. It's really pretty cool.

A little frustrating setting up a development environment, though. If you come up with a good solution, please blog that. Extra points given if it's not Eclipse based :-) Double extra points if it IS IDEA based...

Unknown said...

Haven't used it, but there is an eclipse plugin available. Check out http://scala.epfl.ch/docu/eclipse/. The Scala Plugin requires version 2.0 or newer of the Scala distribution and has been tested with version 3.2 or newer of Eclipse. (from the plugin page)

Iouri Goussev said...

I am yet to try it, but Workingmouse made a plugin for IDEA http://www.workingmouse.com/research/IntelliJIdea%2DScala/