Quantcast
Viewing all articles
Browse latest Browse all 10

Scala as an alternative to Java: Part 1

The end of the semester is drawing near and that means that my independent study in programming languages is also coming to its end. This course has helped me learn a lot about programming languages. Before it I was mildly interested in programming languages, but over the course of the semester I’ve been introduced to many new and innovative ideas and I’ve become convinced that studying programming languages is something I want to continue.

At the same time, I’ve also become interested in virtual languages and the benefits they offer. Over the past few years the Java virtual machine has grown in prominence and there has been a corresponding rise in the number of different languages that can run on the JVM. I’ve never been a big fan of Java, though I will admit that it does have some advantages. With about a month left till I have to go home, I think it’s time to look seriously into an alternative for Java on the JVM. Though there are a number of interesting candidates (Scala, Groovy, Fortress, JRuby, Clojure) the one I’m most interested in looking into is Scala. I’m only just starting to use it, but here are some features of the languages that I’m looking forward to using:

1. Uniform object and static type system

One of my biggest gripes about Java is that it isn’t purely object-oriented. Java is a mix of hierarchical class systems, primitive types (ints, floats) and reference types. Though this isn’t always a program, it can be messy and there can be times when you would really like to subclass one of the primitive types, but you simply can’t. Java is statically typed, but again, not quite. Though type-casting can be quite handy sometimes, I can’t help feeling that it’s somehow ‘unclean’ and not very aesthetically pleasing. And it’s not very safe either. In Scala everything is an object. For examples integers are instances of scala.Int. That lets you subclass base types. It also has strong static typing, as well as type inference. I’m still not very sure about the effectiveness of static typing, but I’ve come to respect it and I’m interested to see how it works.

2. First class functions, anonymous functions and singleton objects

Through my contact with Scheme, Lisp and ML, I’ve come to love functional programming. Some languages like Python do a good job intersecting object oriented and functional programming styles. I’m interested to see how I can adapt my functional programming habits to work in a object-oriented, strongly typed system like Scala. The ability to create Singleton objects without having to go through the hassle of building a container class and instantiating it is something I missed more than once in Java that will lead to some more productive code. As a price, I’ll have to give up Java’s static fields and methods, but I think that should be an acceptable price.

3. Traits and Multiple Inheritance

Java doesn’t support multiple inheritance and I somewhat support the reasons why. However there are workarounds, but none of them have looked very appealing to me. I’ve never been in a position where I really needed multiple inheritance, but I can understand the situations where it would be helpful. Scala allows reusing code from multiple classes using traits which are similar to Java’s interfaces, but can contain code and not just definitions. There are some rules to be followed, but it looks like something that could be a powerful tool

4. Method Overriding

Method overriding is something that is sorely missed by a lot of Java programmers and the lack of it contributes to Java’s verbosity. The concept is simple: if you have a Real numbers class, you can redefine the ‘+’ operator do real addition if overloading is supported. But in Java, you would have to create a class method, say add() which would handle addition. I’ve become a supporter of the idea that things which do you the same thing should look the same (conversely, different things should look different), and overloading is a step in the right direction.

In the near future…

I’m going to be exploring more about Scala. Each of the above is worth an entire post in itself and I’m sure there will be more things I’ll discover on the way. I’m going to be adding to this series at regular intervals so stay tuned.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 10

Trending Articles