Scala, a higher level language on the JVM
Created by the LAMP group at the Ecole Polytechnique Fédérale de Lausane (EPFL), led by Professor Martin Odersky, Scala is a universal programming language designed to allow a simple and concise code. It is close to Java with features from various other languages but also brings innovations and simplifications, which is reminiscent of JavaScript. This translates into a slower compilation.
It is inspired by object-oriented as well as functional programming languages. According to the authors, the Scala code is two to three times more concise than the equivalent code in Java. But it is also possible to write code as unreadable as Perl. As Perl moreover, it allows to do the same thing in very different ways.
The code may be compiled to Java or .NET bytecode and it is compatible with theirs respective runtime. In February 2015, the Scala.js compiler to JavaScript is declared usable in production.
The scala word is short for SCAlable LAnguage and means stairs in Italian, which symbolizes programming by steps.
The compiler scalac is licensed under BSD. Since 2014, the main developers of this compiler are working on a new language, Dotty, a simplified version of Scala, which should help to remove difficult problems to improve this compiler.
On March 14, 2017, Scala Native is announced. This involves replacing the Java virtual machine with LLVM and thus creating executable binaries. It accompanied by a complete library but still requires Java to operate.
In May 2021, comes Scala 3, offering simplifications in the syntax. It is possible to write code using indentation like Python does in place of braces. Braces are now optional, but you can still use the classic syntax.
Features of Scala
You should know that many elements of language such as for and return do not behave as we used to in previous languages​​.
Syntax and constructs:
- Functional: Functions are values.
- Object-Oriented: all values are objects.
- Statically typed.
- Declaration are reversed compared to C: the name of a variable is given and then its type is defined.
- A function is defined by the def operator and the header is near that of Pascal.
- No terminator at end of instructions such as ";" in Java (but may be optionally added).
- The val type is equivalent to a constant, but it can be initialized with a variable.
- No break and continue.
- A class is defined with the constructor as header, which is more concise. See examples.
- Traits are like interfaces in Java, but whose methods can be defined or not. When a trait is used to complete a class, one must define the methods that are not. So their action depends on the context of use.
- Actors are concurrent objects, wich communicate by exchanging messages, synchronously ou asynchronously. An actor is defined by a list of message it can receive. It can react to a message by sending another, so a system of exchanges is created.
- Nested functions.
- Lot of collection types: List, Vector, Set, Map, Seq, Pair, Triple.
- XML code may be included in the source, like in the Scriptol language.
- Scala 3 offers a syntax similar to that of the Python language.
More abilities:
- Anonymous functions.
- First-order functions.
- Nested functions.
- Generic Classes.
- We define a case class for pattern matching, ie to select a class from others in a list depending on a condition.
- Polymorphic methods. Argument may have various types.
- Extensible: A method can be used as an operator.
- Programming abstractions.
- Regular expressions defined by procedural code.
- and many other original features ...
Sample code, displaying "Hello World!":
object HelloWorld { def main(args: Array[String]) { println("Hello World!") } }
Defining a class:
class car(speed:Integer, name:String) { ... body of the constructor and the class ... }
Why use Scala?
Scala facilitates the gradual evolution of the programs over time.
Scala code can easily interface with existing Java code. It is compiled into Java bytecode. We can use both languages in a single program, Scala providing the advantages of being more concise.
Scala allows you to easily process XML documents.
The social site Twitter has left the Ruby language for backend and implemented its services in Scala, to take advantage of the scalable nature of the language. The site is gradually increasing its audience and offer new services over time, hence the need for a language that facilitates the expansion of programs without rewriting.
Tools and documents
- Scala
Official site of the language. Download the compiler and reference manual. - Getting started
How to build and and run a simple program. - Scastie
Online console to try Scala code. - Scaladex
Directory of all the libraries of the language. - Scala.js
Compile Scala to JavaScript 5.1. It is possible to use JS libs, but many conversions are required for types and formats, so it is something complex. - Scala For Android allows to build application for smartphones.
- The Empathica experience
Leaving C# and other Microsoft's tools for Scala, an experience. Among other reasons, the benefit is in the readability of the code, with actors for example.