Scala – Getting Started

I’ve been interested in learning more languages for quite some time, and recently I decided that it’s about time to add another one. Most recently I added Ruby to the mix, and I feel like I know it just well enough to get by. Modules are still a mystical and annoyingly complex box, but for my needs (throwing together scripts, and doing automation testing) I know it well enough for now. This time around I’ve chosen Scala. Scala is a derivation of Java. Or an evolution of it, I’m not really sure which term applies better.


Setting up an environment to start playing is easy. In OS X, you should already have Java 6 without needing to do anything special. If you want to have a newer version of Java, I’m sure it won’t hurt, but for just learning the basics of Scala, I’m sure Java 6 will be fine.

  • Download the latest version of Scala here.
  • Unzip it somewhere. I put it in  ~/sd/scala-2.11.2/
  • Make some modifications to your  ~/.bash_profile

Now we can get into the Scala REPL just by opening a terminal and typing  scala

You’ll also need to setup sbt, which is the scala build tool. I personally use IntelliJ, but even with IntelliJ’s scala/sbt support, it took me a long time to get up and running compared to just installing sbt. The easiest way is to use Homebrew or Macports


Variables and Constants

In Scala, mutable and immutable variables are declared differently.

We can also declare variables using type inference.

You’ll also notice that I haven’t used a single  ;. This is because, as is the growing norm, a ; is optional. You only have to use one if you want to put multiple statements on the same line.


Method definitions look to me like a mashup of Swift and Ruby. A method definition uses the following structure

We also don’t need to explicitly put the return type of a method. We can once again use type inference. But there is a catch. Sometimes, using type inference can cause some programming mistakes, so my personal recommendation. Use type inference for variables and constants, but never ever, ever use it for the return value of a declared, named method.


In more technical terms, methods are declared with the following syntax

  • The word ‘def’
  • The name of the method
  • parameters separated by a ‘,’ of the following form
    • variable name : variable type
  • Optional : with return type
  • =
  • Implementation of the method
    • This could be an inline method, or we can use { }

It is also important to note that there is no need for an explicit return in your methods. If you don’t have an explicit return, then the last line evaluated in your method will be the return value. You can still use explicit returns, but it seems that to have an explicit return, you must have an explicit return type. Also, return can cause some odd behavior from within functions inside of methods. I’m not entirely sure I understand the implications of return at this time.


Leave a Comment

Your email address will not be published. Required fields are marked *