Scala runs...

  • on the JVM
  • on JavaScript in your browser
  • natively with LLVM beta

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.

Author.scala
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: java.io.File): List[Author] = ???
}
App.java
import static scala.collection.JavaConversions.asJavaCollection;

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

    public void sortAuthors(List<Author> authors) {
        Collections.sort(authors);
    }

    public void displaySortedAuthors(File file) {
        List<Author> authors = loadAuthorsFromFile(file);
        sortAuthors(authors);
        for (Author author : authors) {
            System.out.println(
                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 person.name
     | }
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.

Concurrent/Distributed
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.

Traits

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.

Traits
abstract class Spacecraft {
  def engage(): Unit
}
trait CommandoBridge extends Spacecraft {
  def engage(): Unit = {
    for (_ <- 1 to 3)
      speedUp()
  }
  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.

Scala
val people: Array[Person]

// Partition `people` into two arrays `minors` and `adults`.
// Use the anonymous function `(_.age < 18)` as a predicate for partitioning.
val (minors, adults) = people partition (_.age < 18)
Java
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)
        minors.add(person);
    else
        adults.add(person);
}

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

Online Courses

Functional Programming Principles in Scala

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Functional Program Design in Scala

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Parallel Programming

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Big Data Analysis with Scala and Spark

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Functional Programming in Scala Capstone

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Effective Programming in Scala

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Programming Reactive Systems

  • Free (optional paid certificate)
  • New sessions starting every 2 weeks!

Upcoming Training

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

BLOG

Scala GSoC 2021 is finished: meet the team!

Monday, September 27, 2021

Google Summer of Code (further: GSoC) 2021 edition is completed 🎉

In this post we want to celebrate the students and mentors who made Scala GSoC 2021 a success!

GSoC is an international annual program in which Google awards stipends to University students who successfully contribute to open source projects. The goal is to help open source projects identify and bring in new developers by offering students the opportunity to gain real-world software development experience.

Scala GSoC 2021: Introducing students and projects

The Scala Center had the honor to organize Scala GSoC 2021 and the pleasure to help coordinate 4 brilliant students and their mentors. In this section we introduce and welcome them in our Community!

Add synthetics and symbol information for semanticdb in Scala 3

Student: Rikito Taniguchi

Mentors: Tomasz Godzik, Chris Kipp, Jamie Thompson, Aleksander Boruch-Gruszecki

SemanticDB is a data model for semantic information such as symbols and types in Scala programs. It is widely used for developing Scala’s devtools such as scalafix and Metals. However, the SemanticDB extractor for Scala3 was a work in progress, and some features in devtools were unavailable for Scala3. This project focuses on enriching SemanticDB with additional information from the Scala3 compiler in order to improve the developer experience of Scala 3 developers You can read more here

Rikito’s work enables a huge amount of features in tools used for working with he Scala 3 code including Metals and Scalafix. There is nothing currently that think Rikito can improve as his work has been completely stellar.

cit. Tomasz

Cross compiling “Shapeless 2” to Scala 3

Student: Katrix

Mentors: Julien Richard-Foy, Aleksander Boruch-Gruszecki

Shapeless is a crucial dependency of many popular Scala libraries. This project explores how the Scala 2 version of the library could be cross-built for Scala 3 by porting some parts to Scala 3. It aims at helping library maintainers to adopt Scala 3 faster. You can read more here

Congratulations for your work on Shapeless, especially for being so autonomous in this project!

cit. Julien

Implement support for ScalaPy in Scala 3

Student: Mykola Medynskyi

Mentors: Shadaj Laddad, Anatolii Kmetiuk

ScalaPy is a library that enables Scala programs to use Python libraries. This project brings support for Scala 3 to the library to enable users to leverage all the new cool features that the new language versions ships while relying on powerful and well known Python libraries. You can read more here

Mykola’s contributions bringing support for Scala 3 are really impressive and will enable many new research and production use cases in the future!

cit. Shadaj

A special thanks goes to Zhendong Ang who co-menthored Mykola and collaborated on the project for his semester project at EPFL.

A Python - Scala integrated environment with Almond, Ammonite and ScalaPy

Mentors: Anatolii Kmetiuk, Alexandre Archambault

ScalaPy is a library that enables Scala programs to use Python libraries. This project focuses on improving developer experience by simplifying the setup process, providing autocomplete functionalities in Almond and Ammonite, enriching the documentation and object displays. You can read more here

This work kick-started a better integration of the ScalaPy library in Scala notebooks, making it easier to use Python libraries from there, and helping the future prospects of both Scala notebooks and the use of Python libraries from Scala.

cit. Alexandre

Thank you, Mentors!

Scala GSoC 2021 would not have been possible without our Mentors that dedicated their time and energy as volunteers to guide new contributors. Thank you mentors for the great work. We hope to see you in the next edition(s)!

Here are a few of the comments that students submitted:

Both mentors are very helpful to me in this project and I learned a lot from them (Hi Alex and Toli!)

Thank you so much Tomasz for mentoring me [..] Thank you so much for helping me with communication: pinging to reviewers, inviting Jamie to the call, and always trying to move the project forward. I admire your communication skill. and thank you so much Jamie for reviewing my Pull Requests!

Shadaj Laddad and Anatolii Kmetiuk are great mentors. Anatolii is incredibly skilled in explanations. He perfectly explained in 30 minutes the basics of metaprogramming in Scala which I could not understand for a month before it by reading Scala docs and I am very thankful to him for this. Shadaj is an excellent coordinator. He has a great vision of the whole project, possible problems in it, and how to fix them. Also, his code reviews helped me to learn how to make my code better and I am also very thankful to him. It is a pleasure to work with them.

We also want to thank our candidate Mentors who accepted the call of the Scala Center to propose interesting project ideas and who volunteered their time:

  • Wojciech Mazur
  • Sébastien Doeraene
  • Adrien Piquerez
  • Eric Loots
  • Meriam Lachkar
  • Dale Wijnand
  • Fengyun Liu
  • Lars Hupel
  • Krzysztof Romanowski
  • Mateusz Ziarko
  • Felix Mulder
  • Noel Welsh

How to get involved in Scala GSoC 2022

Google has not disclosed information about the 2022 edition of GSoC yet. We can however learn from previous editions how to prepare for it and, if it follows the same general organization of 2021, the event might start as soon as February 2022, so it’s not too soon to start. Here is how:

For Open Source Project Maintainers

First and foremost it is important to keep an updated list of project ideas: open source projects willing to be part of the GSoC should maintain GitHub issues, project boards or simple text files containing proposals for students. To make your project more approachable, offer projects with different levels of complexity and various topics. You can find useful information about project proposals at https://google.github.io/gsocguides/mentor/defining-a-project-ideas-list

You should also identify at least one mentor and one backup mentor for each proposal. This will speed up the application process but also make the project visible to students before the event starts, increasing the chances that motivated students will take the opportunity to work on your repository.

Communication is fundamental too: candidate mentors are encouraged to communicate about their participation in GSoC and about project ideas throughout the year. Be responsive to students approaching you on the project channels (GitHub issues, Discord, Gitter, Twitter…).

If you maintain a Scala library and would like to be part of Scala GSoC 2022 with the help of the Scala Center, do not hesitate to contact us at scala-gsoc@epfl.ch!

For Students

If you are a student and want to be involved in Scala GSoC 2022, get in touch with the maintainers of the projects that you would like to contribute to. The GitHub repo or project site will contain information about communication media such as Discord, Gitter, Twitter or good-old email.

Remember that most maintainers volunteer their free time for open source, so do not expect immediate replies. You can find some tips about making first contact at https://google.github.io/gsocguides/student/making-first-contact

You can also start to think about project proposals (you can find a lot of useful info at https://google.github.io/gsocguides/student/writing-a-proposal) and discuss them with project maintainers.

Feel free to contact us at scala-gsoc@epfl.ch if you get stuck along the way, we are happy to help within our capacities.

Even more: Brief history, stats, and goals

The Scala organization has been involved in GSoC for many years thanks to the efforts of Martin Odersky’s lab (called LAMP) at EPFL:

Student participations by year

In 2021 the Scala Center decided to revive this long tradition of successful projects (only 2 students in 9 editions did not complete their projects) with help of the maintainers of open source Scala libraries.

For this year’s edition we gathered 28 project ideas and 20 candidate mentors motivated to support students with such ideas. We received 10 applications and after reviewing proposals and assigning students to mentors, we retained 4 projects to enter the program.

For the 2022 Scala GSoC edition, we are aiming to attract at least 10 new students and we invite you to help us by

  • offering to be a mentor
  • letting us know if you have a student interested to participate
  • spreading the word!

Twitter Feed

See more tweets, or

Follow Scala on twitter

The Scala language is maintained by

  • Scala Center
  • Lightbend
  • VirtusLab

Scala Center is supported by

EPFL Goldman Sachs 47 Degrees Twitter Spotify Lunatech Your company