The most important feature of Scala is that it tends to be very enjoyable. This is because there is no boilerplate code and a lightweight syntax, but at the same time it has the safety of a strong static type system.
Let’s say hello to Scala by modifying the Hello.scala
file either in a text editor or in an IDE. With SBT the code is automatically run after the file is changed and saved (Ctrl + S) when you prefix commands with the “~” tilde character. For example, run sbt then from the sbt prompt enter the sbt command “run“. With the”” tilde command prefix sbt will compile and run the app on each source file change.
In Scala, variables can either be mutable or immutable. An immutable variable is a variable that cannot be modified after it has been created. There are a number of reasons for using immutable variables. For instance, they are inherently thread-safe. Another reason is that they are easier to reason about because you know the value cannot change. The best practice is to start off with an immutable variable and only make it mutable if needed.
To create an immutable variable, you preface it with the val
keyword and mutable variables are created with the var
keyword. In the Hello.scala
file let’s start by creating an immutable String
variable called first
and printing it out:
If you are running this using sbt ~run
, after you save the file, go to the terminal window to see the results.
To understand what it means to be immutable, try assigning the variable first
to a new value:
When you save it, the compilation of the source file will fail:
Now let’s change first to be a mutable variable by going back up and changing the val
to var
and saving the program. This time the program compiles and if you print out the first variable after changing the value you will see the changed value.
Continuing the exploration of Scala features, let’s look next at type inference, which means the Scala compiler infers the type of a variable. This allows the programmer to omit certain types annotations. To see this in action change the declaration of the first variable by removing the type:
What you will notice is that the program still compiles. The Scala compiler has inferred first as a String
because it was assigned a String
value. This is different than a dynamic language like JavaScript, which allows any variable to contain any value, e.g. first set a variable to an integer value then change it to a string. In Scala you cannot change the type of the variable after it has been declared. For example, try assigning first
to a number:
You will get a type mismatch error saying the compiler found an Int
but it required a String
.
Be sure to delete this line so your sample will continue to compile.
Type Inference is really powerful in Scala and can be used to infer the type of variables, methods, functions, and other expressions. It should be used with some caution though, as the Scala Style Guide (http://docs.scala-lang.org/style/types.html) says:
“Use type inference where possible, but put clarity first, and favor explicitness in public APIs.”
Another feature that makes Scala unique is that it is fully expression-oriented. For example, let’s create an Int
(an integer type in Scala) variable and assign it to the result of a basic calculation:
This assigns a value of 98 to second. Notice that it does not require an explicit return statement and instead takes the last line to be the value returned. In the same way, an if-else is an expression in Scala and yields a result. We could create a String
variable displayFlag
that is the result of a basic if-else test:
Or, in shorthand notation, this could be expressed as:
Lets now dive in-depth into some more Scala features.