There are new languages that come about all of the time. Lately it seems that most of the languages du jour are dynamic or functional languages. We have all seen a lot of talk about languages such as Ruby, Python, F#, Groovy, Erlang, and Clojure. All of these languages represent a huge shift in programming paradigm from many of the current “corporate” languages such as C# and Java. They also all represent a significant syntactical difference from these languages.
What is Scala?
Scala on the other hand is a little bit different. From a syntax point of view, it is quite similar to both C# and Java. From a paradigm perspective it does represent a shift, but at the same time it allows for a style of development that will be quite familiar to any C# or Java developer. In fact, Scala was designed to run on the JVM and it even shares its type system with Java (although it adds namespaces of its own) so that it can easily interoperate. A version of Scala has been developed to run on the .NET platform, although the development effort is a bit behind the JVM version (from what I have heard though, Microsoft has provided a grant to help development of the .NET port, if anyone has any more information on this, please let me know).
Okay, so we know that Scala has a similar syntax and type system to C# and Java, but what is really important is where Scala differs. As we already said, it is still a statically typed language, just like C# and Java, but it uses different types of functionality in order to give it a more dynamic feel. It also has much stronger type inference, which allows the developer to not have to specify types as often as they would in a C# or Java program.
Scala adds a number of other significant advancements with everything from closures, immutability, mixins, pattern matching, lazy evaluation, partial function application, etc… In this series of articles, we are going to start with Scala at a very basic level, and then progress into object oriented programming, and lastly touch a bit on functional programming with Scala. All along the way we will compare and contrast Scala to C#, so that you can easily see how they compare.
So after all this you might be asking, “Why Scala?” Well, for one, I think it is important to learn a new language every so often (so do some other guys). Last year for me I really dug into Ruby, and created a similar series of posts titled Learning Ruby via C#. Beyond that though, I think that Scala is an excellent platform for seeing the future evolution of the languages that most of us use to this day. In fact, C# already has many more of the features that are available in Scala than Java does. I think that as C# evolves, we will see a few of the features in Scala creep over into C# in some form.
Some people have criticized Scala and have said that it is a “kitchen sink” language. Critics will argue that because it leans heavily in both the object oriented and functional worlds, that it does neither well. I don’t really agree with that statement for a few reasons. First, I am a firm believer in the fact that the lines of languages are blurring. To call a language static, dynamic, or functional has less and less meaning every day. Secondly, Scala is not trying to be a strong functional language. Scala is at its heart an object oriented language with a healthy does of functional features mixed in to provide a good amount of flexibility and power. Because of this Scala has a lot of surface area, and so I think from that perspective the criticisms are accurate. For me though, the amount of features in the language is just a sign of the changing needs of developers.
Before we go any further we are going to have to get an environment setup that will allow us to start developing with Scala. I prefer to use an IDE since, after all, I am a Microsoft developer. 🙂 Because of the fact that the .NET port of Scala is currently pretty hard to use and get setup, we are going to use the JVM version. Right now, the NetBeans plugins for Scala are some of the best and most mature, and so we are going to use those. If you prefer to use Eclipse, there are plugins available for it as well.
The first thing we are going to do is install Scala. To do this we will visit http://www.scala-lang.org/downloads and download the appropriate version of Scala for our machine. In my case the Windows version. You’ll want to extract the contents of this zip to somewhere on your hard drive, and I choose to put it in a c:\tools\scala-2.7.6.final\ directory.
You can then go to this folder in a command prompt and enter the “bin” folder. Just type “scala” to verify that you have the scala REPL up and running properly:
Next we need to add this bin folder to the path in windows. To do this we can go to System Properties > Advanced Tab, and then click “Environment Variables” button at the bottom. Add “;C:\Tools\scala-2.7.6.final\bin” to the end of the path variable, but you’ll have to reboot for this to take effect. But don’t do that yet, leave the environment variables dialog open, we will need it later.
The next thing that we are going to do is install NetBeans. The latest version of the Scala plugin works with the 6.8 release of NetBeans which, as of 10/12/2009, is only in a Milestone 2 release. It is however quite stable and usable. It can be found at http://bits.netbeans.org/netbeans/6.8/. You’ll want to grab the latest version of 6.8 that is available. If the link above doesn’t work, then just go to http://www.netbeans.org/downloads/index.html and look for the link to 6.8, or whatever the latest version happens to be. You can download whichever configuration you want, I normally just install the “All” version. You can simply run the installer, follow the prompts and you’ll have NetBeans up and running in no time.
The next thing that you’ll need to do is grab the Scala plugin for NetBeans. This can be found at http://sourceforge.net/projects/erlybird/files/. The current file is nb-scala-6.8v1.1.0m2.zip. If you are using NetBeans 6.7 then just drop down the nb-scala section and find the zip file that ends with 6.7v1. After you download the zip, just place the extracted files into a folder. I just created a plugins\Scala folder under my netbeans install directory, which is located at C:\Program Files\NetBeans 6.8 M2\ on a Windows machine.
Once you have unzipped the files into a folder, simply run NetBeans and go to Tools > Plugins:
From the window that pops up, you can just click on the “downloaded” tab:
Just click on the “Add Plugins…” button and find and select all of the files that you unzipped earlier.
Then all you have to do is click open, then click install:
Next you need to tell NetBeans where to find Scala. This is done by creating a SCALA_HOME environment variable and pointing it to “C:\Tools\scala-2.7.6.final\”. We will do that using the dialog that I hope you left open earlier! Now you can restart NetBeans to pick up your changes. And you should be up and running!
Now you can go to File > New Project:
And then all you need to do is click “Scala” and choose “Scala Application”:
Then you can click “Next” and “Finish” and you’ll be greeted with a default Scala application:
As you can see, the syntax is very similar, but at the same time a bit foreign. I’m not going to delve into the syntax in this post, but I do hope you’ll come back and join me for the next entry in the series where we will start to look at Scala’s variables and type system! Until next time…
Loved the article? Hated it? Didn’t even read it?
We’d love to hear from you.
A month or so ago, I decided that Scala would be the next language I tried out. Good to see that I’ll have a guide through the process (and from a C# perspective to boot 🙂 )
Thanks (a lot!) for posting this
I hope that this will be as good series of articles as your Ruby was…
It wasn’t so good to compare C# and Ruby (as you can always make things so many ways), but for showing main features of Ruby it was. And that was the point of the series, right?
Altough something you didn’t really talk anything about: the user experience. How about intellisense etc. A part of C# power is the Visual Studio IDE (with ReSharper). It doesn’t matter if the syntax is a bit longer if I just press two keys to make it.
(Well ok, in Func<Func<List<int>, List<int>>, Func<int>> the syntax does matter.) 😀
Leave a comment