With this issue we launch a new series on the Scala programming language by Venkat Subramaniam.
Greetings, and welcome to this new series on Scala. Over the next several issues of PragPub we’ll learn quite a bit about this powerful language on the JVM. And why would we want to do this?
First, it’s fun. If programming is your passion, you don’t need any further reason.
Let’s say you’ve been programming in Java for several years, and you’ve gotten really good at it. By now you wake up each morning with the entire code for the day’s task already written in your head. All that’s left is to type that in and get it compiled. You’re such a pro that you don’t even get any compilation errors any more.
Passionate programmers thrive to learn and continuously figure out ways to improve their craft. If programming has turned into a chore, Scala can help.
Scala will add spice to your programming life. In Scala you will write less code to do the same work. You can get more real programming done while spending less time battling the ceremonies imposed by languages or their compilers. Scala gets you directly to where you want to be, so you can be expressive, concise, and productive at writing code.
Let’s be fair—Java has evolved over the years, and has really come a long way. But the good parts of Java are in the layers below the language—the Java virtual Machine and the ubiquitous library. The Java language itself is showing its age.
I see languages as vehicles that allow us to navigate the terrain of the platform. It is time to trade in that clunky gas guzzler for a more efficient vehicle that will allow you to get around faster and more efficiently. Scala is one of the languages on the JVM that can provide exactly that.
In writing Scala, Martin Odersky set out to marry two powerful paradigms: the object oriented and functional styles of programming. Scala is fully object oriented. But it is really a hybrid—a functional object-oriented language with sensible static typing with a good dose of type inference.
One of the key strengths of Scala is in its conciseness.
Let’s compare Scala’s conciseness with Java’s. As an experienced Java programmer, suppose you have volunteered to teach Java to a captive audience who were told that Java is simple (and we know it is, because so many books on Java have told us so). You decide to start with a simple “hello world” example. So, you type:
A hand goes up: “What’s public?”
You say: “Eh, you don’t need to know that right now. Let’s proceed.”
A hand goes up again: “What’s a class?”
You say again: “You don’t need to know that right now either, hang in there.”
Right there a glimpse of hope comes into in the eyes of your audience. You finally got to “HelloWorld”... but that hope comes down crashing when you proceeded to the next line:
Another hand goes up: “So, we’re not supposed to worry about what static is right now either?!” You know the pain.
Let’s try that in Scala. Open a command prompt and simply type the following:
In context, that looks like this:
You opened the Scala REPL (Read Evaluate Print Loop), or interactive shell, and simply typed println("hello world") and it printed it. If you want to put that into a file, it is not much different. Create a file named helloworld.scala and type the following in it and save.
Now, to run it, type:
and you will see the output:
You can run Scala code as a script or go through the compilation steps as in Java (more about these later).
Let’s write a program in Java that greets our user and after a delay of one second prints a goodbye message.
The Java compiler forces you to handle the exception from the sleep method. You have to either catch it or propagate it up the chain by explicitly placing a throws clause. Let’s try that example in Scala:
Save that into greet.scala and run it:
The Scala code is concise, but there’s more:
1. The Scala compiler did not force you to handle the exception. If you don’t handle the exception it’s automatically propagated up the call chain. You have seen what programmers do when they’re forced to handle exceptions—they show who’s powerful to the compiler by putting in empty catch blocks. Scala expects us to be responsible and do the right thing rather than forcing it on us.
When you program in Java, you feel like you’re working for the compiler. When you program in Scala, the compiler works for you.
2. We used the ternary operator ?: in Java to decide the name. Java provides if statements and then the ternary operator in addition. Scala does not have the ternary operator, because if is not a statement in Scala. It is an expression that can return values. This is a common theme you will find in Scala. It provides fewer but more powerful constructs. Programming in Scala is like playing with the Lego toys: the small building blocks can be configured in endless ways to form bigger structures.
3. Scala did not require you to create a class. As we will see later, you can create classes in Scala, but you do so only if you want to, not because you’re forced to. In Scala, simple things are simple and complex things require marginally more effort.
We’ve merely scratched the surface of Scala (hey, this is the first article in the series). But, I hope to have whetted your appetite for the language. So far I have painted a good picture of Scala. To be objective, the syntax of Scala can be quite intimidating to beginners. Scala is not for the faint at heart. You have to get used to the syntax and some of the quirks of the language. I have heard many programmers new to Scala complain, so now I believe
You Don’t Pick Scala, Scala Has to Pick You.
In a sense, Scala is like the dragon in Avatar. It will try to kill you, but if you master it, you can fly great distances with it and have a wonderful time.
My objective is not to show you everything you can do with Scala. Instead, we will walk through how effectively we can apply this language to do common operations we do in day-to-day programming.
I hope you’re excited about this series as much as I am. Until next time, program responsibly. Cheers.
Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., and an adjunct faculty at the University of Houston.
He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with agile practices on their software projects.
Venkat is the author of .NET Gotchas, the coauthor of 2007 Jolt Productivity Award winning Practices of an Agile Developer, the author of Programming Groovy: Dynamic Productivity for the Java Developer and Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine. His latest book is Programming Concurrency on the JVM: Mastering synchronization, STM, and Actors.