Scala runs on...

  • JVM
  • JavaScript in your browser

- with more backends on the way. -

Scala in a Nutshell

click the boxes below to see Scala in action!

Seamless Java Interop

Scala runs on the JVM, so Java and Scala stacks can be freely mixed for totally seamless integration.

Type Inference

So the type system doesn’t feel so static. Don’t work for the type system. Let the type system work for you!

Concurrency & Distribution

Use data-parallel operations on collections, use actors for concurrency and distribution, or futures for asynchronous programming.

class Author(val firstName: String,
    val lastName: String) extends Comparable[Author] {

  override def compareTo(that: Author) = {
    val lastNameComp = this.lastName compareTo that.lastName
    if (lastNameComp != 0) lastNameComp
    else this.firstName compareTo that.firstName

object Author {
  def loadAuthorsFromFile(file: List[Author] = ???
import static scala.collection.JavaConversions.asJavaCollection;

public class App {
    public List<Author> loadAuthorsFromFile(File file) {
        return new ArrayList<Author>(asJavaCollection(

    public void sortAuthors(List<Author> authors) {

    public void displaySortedAuthors(File file) {
        List<Author> authors = loadAuthorsFromFile(file);
        for (Author author : authors) {
                author.lastName() + ", " + author.firstName());

Combine Scala and Java seamlessly

Scala classes are ultimately JVM classes. You can create Java objects, call their methods and inherit from Java classes transparently from Scala. Similarly, Java code can reference Scala classes and objects.

In this example, the Scala class Author implements the Java interface Comparable<T> and works with Java Files. The Java code uses a method from the companion object Author, and accesses fields of the Author class. It also uses JavaConversions to convert between Scala collections and Java collections.

Type inference
scala> class Person(val name: String, val age: Int) {
     |   override def toString = s"$name ($age)"
     | }
defined class Person

scala> def underagePeopleNames(persons: List[Person]) = {
     |   for (person <- persons; if person.age < 18)
     |     yield
     | }
underagePeopleNames: (persons: List[Person])List[String]

scala> def createRandomPeople() = {
     |   val names = List("Alice", "Bob", "Carol",
     |       "Dave", "Eve", "Frank")
     |   for (name <- names) yield {
     |     val age = (Random.nextGaussian()*8 + 20).toInt
     |     new Person(name, age)
     |   }
     | }
createRandomPeople: ()List[Person]

scala> val people = createRandomPeople()
people: List[Person] = List(Alice (16), Bob (16), Carol (19), Dave (18), Eve (26), Frank (11))

scala> underagePeopleNames(people)
res1: List[String] = List(Alice, Bob, Frank)

Let the compiler figure out the types for you

The Scala compiler is smart about static types. Most of the time, you need not tell it the types of your variables. Instead, its powerful type inference will figure them out for you.

In this interactive REPL session (Read-Eval-Print-Loop), we define a class and two functions. You can observe that the compiler infers the result types of the functions automatically, as well as all the intermediate values.

val x = future { someExpensiveComputation() }
val y = future { someOtherExpensiveComputation() }
val z = for (a <- x; b <- y) yield a*b
for (c <- z) println("Result: " + c)
println("Meanwhile, the main thread goes on!")

Go Concurrent or Distributed with Futures & Promises

In Scala, futures and promises can be used to process data asynchronously, making it easier to parallelize or even distribute your application.

In this example, the future{} construct evaluates its argument asynchronously, and returns a handle to the asynchronous result as a Future[Int]. For-comprehensions can be used to register new callbacks (to post new things to do) when the future is completed, i.e., when the computation is finished. And since all this is executed asynchronously, without blocking, the main program thread can continue doing other work in the meantime.


Combine the flexibility of Java-style interfaces with the power of classes. Think principled multiple-inheritance.

Pattern Matching

Think “switch” on steroids. Match against class hierarchies, sequences, and more.

Higher-order functions

Functions are first-class objects. Compose them with guaranteed type safety. Use them anywhere, pass them to anything.

abstract class Spacecraft {
  def engage(): Unit
trait CommandoBridge extends Spacecraft {
  def engage(): Unit = {
    for (_ <- 1 to 3)
  def speedUp(): Unit
trait PulseEngine extends Spacecraft {
  val maxPulse: Int
  var currentPulse: Int = 0
  def speedUp(): Unit = {
    if (currentPulse < maxPulse)
      currentPulse += 1
class StarCruiser extends Spacecraft
                     with CommandoBridge
                     with PulseEngine {
  val maxPulse = 200

Flexibly Combine Interface & Behavior

In Scala, multiple traits can be mixed into a class to combine their interface and their behavior.

Here, a StarCruiser is a Spacecraft with a CommandoBridge that knows how to engage the ship (provided a means to speed up) and a PulseEngine that specifies how to speed up.

Switch on the structure of your data

In Scala, case classes are used to represent structural data types. They implicitly equip the class with meaningful toString, equals and hashCode methods, as well as the ability to be deconstructed with pattern matching.

In this example, we define a small set of case classes that represent binary trees of integers (the generic version is omitted for simplicity here). In inOrder, the match construct chooses the right branch, depending on the type of t, and at the same time deconstructs the arguments of a Node.

Pattern matching
// Define a set of case classes for representing binary trees.
sealed abstract class Tree
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
case object Leaf extends Tree

// Return the in-order traversal sequence of a given tree.
def inOrder(t: Tree): List[Int] = t match {
  case Node(e, l, r) => inOrder(l) ::: List(e) ::: inOrder(r)
  case Leaf          => List()

Go Functional with Higher-Order Functions

In Scala, functions are values, and can be defined as anonymous functions with a concise syntax.

val people: Array[Person]

// Partition `people` into two arrays `minors` and `adults`.
// Use the higher-order function `(_.age < 18)` as a predicate for partitioning.
val (minors, adults) = people partition (_.age < 18)
List<Person> people;

List<Person> minors = new ArrayList<Person>(people.size());
List<Person> adults = new ArrayList<Person>(people.size());
for (Person person : people) {
    if (person.getAge() < 18)

Run Scala in your browser

Scastie is Scala + sbt in your browser! You can use any version of Scala, or even alternate backends such as Dotty, Scala.js, Scala Native, and Typelevel Scala. You can use any published library. You can save and share Scala programs/builds with anybody.

Run Scala code interactively

Scala ecosystem

The Scala Library Index (or Scaladex) is a representation of a map of all published Scala libraries. With Scaladex, a developer can now query more than 175,000 releases of Scala libraries. Scaladex is officially supported by Scala Center.

The Scala Library Index

What’s New


Accessibility of the Scala Compiler

Monday, March 19, 2018

In 2001, as a university undergraduate, a friend and I were lucky enough to get the opportunity to meet the physicist Stephen Hawking at a small private reception after he gave a public lecture in Cambridge.

Whilst the lecture was itself deeply interesting, two things struck me more than anything else from that evening: the first was the warmth of Prof Hawking, who went from that lecture theatre having spoken to an audience awed by his explanations of black holes and branes in eleven-dimensional spacetime, to join us in a quiet side-room where he talked to my friend, also a wheelchair-user, about the availability of ramps and general accessibility in different buildings in the university; some of the most mundane practicalities of his daily life.

The other thing I observed was the ergonomics of Prof Hawking’s computer, as I stood alongside him and watched him slowly compose sentences, before completing them, and having them spoken. Presented with a monochrome screen with alternating selections of words, he had no greater physical capability than to synchronize several clicks of the computer’s single button to select one word at a time and append it to a sentence he gradually built up. It felt frustratingly slow for the rest of us, all too accustomed to the ease of speech, but for him it was an enabler through which he was able to make huge contributions to scientific research, and through which the rest of us were able to see through a small window into his great—and now sadly lost—intellect.

It is a reminder how easy it is for many of us to take our experience of programming for granted: we can quickly glance over source code or compiler errors on our screens to spot bugs or understand functionality. For blind programmers, the screen is usually replaced by a text-to-speech system, which linearly renders source code and error messages as spoken text.

Unfortunately, for a language like Scala, that audible representation is presented very verbosely, and can be painful to use. An engine such as “espeak” may render a definition like,

def foo[A: Bar](s: String): Int = ...


def space foo space open square bracket capital a colon space bar close square
bracket open bracket s colon space string close bracket colon space int equals

whereas that same information could be conveyed more expediently as,

def foo
type a context bar
from s string to int

During the Scala Center Advisory Board last week, a proposal authored and strongly advocated by Sam Halliday, with help from Rui Batista, was accepted as a recommendation by the board, with a unanimously positive vote from all the members, including 47 Degrees, IBM, Goldman Sachs, Lightbend, Morgan Stanley, SAP, Twitter and Verizon.

The Scala Center will work towards making Scala more accessible. This effort has already received some open-source contributions, and the Scala Center will work hard to ensure that these contributions are seen through to completion, as we experiment with effective ways to improve the output from the compiler through a screenreader. This work will be not be tied to any particular choice of screenreader technology or editor.

This also presents an interesting area of research for improving the ergonomics of programming in an advanced statically-typed language for blind and partially-sighted users, which is by no means a “solved problem”, and offers much room for improvement.

Most of the work the Scala Center does contributes small differences to the lives of many developers in the Scala community. This proposal is different in that it provides a potentially enormous difference to the experience of programming in Scala to a small number of users.

But more than that, it furthers our goal of inclusivity; that Scala is a language which rightly knows the importance of supporting all its users, regardless of physical disability; to make it easier, in particular, for blind and partially-sighted people to become Scala developers, working productively alongside sighted developers, and having the greatest opportunity possible to share their valuable contributions in the field of software development.

Read this proposal here: SCP-016: Accessible Scala.

Twitter Feed

See more tweets, or

Follow Scala on twitter

The Scala language is maintained by

  • Scala Center
  • Lightbend

Scala Center is supported by

EPFL IBM Verizon Goldman Sachs 47 Degrees SAP Twitter Your company