Object-Oriented Meets Functional

Have the best of both worlds. Construct elegant class hierarchies for maximum code reuse and extensibility, implement their behavior using higher-order functions. Or anything in-between.

Learn More

 

Scala began life in 2003, created by Martin Odersky and his research group at EPFL, next to Lake Geneva and the Alps, in Lausanne, Switzerland. Scala has since grown into a mature open source programming language, used by hundreds of thousands of developers, and is developed and maintained by scores of people all over the world.
Download API Docs    
Spiral
Scala
2.12.2

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.

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.

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
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.

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()
}

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.

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 higher-order 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);
}

In the Scala example on the left, the partition method, available on all collection types (including Array), returns two new collections of the same type. Elements from the original collection are partitioned according to a predicate, which is given as a lambda, i.e., an anonymous function. The _ stands for the parameter to the lambda, i.e., the element that is being tested. This particular lambda can also be written as (x => x.age < 18).

The same program is implemented in Java on the right.

Upcoming Events

See more events or add one to our feed

What's New

blog
date icon Tuesday, May 30, 2017

CanBuildFrom is probably the most infamous abstraction of the current collections. It is mainly criticised for making scary type signatures.

Our ongoing collections redesign (blog post, GitHub repo) is an opportunity to try alternative designs. This post explains the (many!) problems solved by CanBuildFrom and the alternative solutions implemented in the new collections.

Transforming the elements of a collection

It’s useful to think of String as a collection of Char elements: you can then use the common collection operations like ++, find, etc. on String values.

However the map method is challenging because this one transforms the Char elements into something that might or might not be Chars. Then, what should be the return type of the map method on String values? Ideally, we want to get back a String if we transform each Char into another Char, but we want to get some Seq[B] if we transform each Char into a different type B. And this is the way it currently works:

Welcome to Scala 2.12.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> "foo".map(c => c.toInt)
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(102, 111, 111)

scala> "foo".map(c => c.toUpper)
res2: String = FOO

This feature is not limited to the map method: flatMap, collect, concat and a few others also work the same. Moreover, String is not the only collection type that needs this feature: BitSet and Map are other examples.

The current collections rely on CanBuildFrom to implement this feature. The map method is defined as follows:

def map[B, That](f: Char => B)(implicit bf: CanBuildFrom[String, B, That]): That

When the implicit CanBuildFrom parameter is resolved it fixes the return type That. The resolution is driven by the actual B type: if B is Char then That is fixed to String, otherwise it is immutable.IndexedSeq.

The drawback of this solution is that the type signature of the map method looks cryptic.

In the new design we solve this problem by defining two overloads of the map method: one that handles Char to Char transformations, and one that handles other transformations. The type signatures of these map methods are straightforward:

def map(f: Char => Char): String
def map[B](f: Char => B): Seq[B]

Then, if you call map with a function that returns a Char, the first overload is selected and you get a String. Otherwise, the second overload is selected and you get a Seq[B]. Before Scala 2.12 such a solution would not have worked well: users would have been required to explicitly write the type of the argument of the supplied f function. In Scala 2.12 type inference has been improved so that it is not anymore necessary.

Thus, we got rid of the cryptic method signatures while still supporting the feature of returning a different type of result according to the type of the transformation function.

Collections’ type constructors with different arities

The collections are hierarchically organized. Essentially, the most generic collection is Iterable[A], and then we have three main kinds of collections: Seq[A], Set[A] and Map[K, V].

It is worth noting that Map[K, V] takes two type parameters (K and V) whereas the other collection types take only one type parameter. This makes it difficult to generically define, at the level of Iterable[A], operations that will return a Map[K, V] when specialized.

For instance, consider again the case of the map method. We want to generically define it on Iterable[A], but which return type should we use? When this method will be inherited by List[A] we want its return type to be List[B], but when it will be inherited by HashMap[K, V], we want its return type to be HashMap[L, W]. It is clear that we want to abstract over the type constructor of the concrete collections, but the difficulty is that they don’t always take the same number of type parameters.

That’s a second problem solved by CanBuildFrom in the current collections. Look again at the type signature of the (generic) map method on Iterable[A]:

def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That

The return type That is inferred from the resolved CanBuildFrom instance at call-site. Both the Repr and B types actually drive the implicit resolution: when Repr is List[_] the parameter That is fixed to List[B], and when Repr is HashMap[_, _] and B is a tuple (K, V) then That is fixed to HashMap[K, V].

In the new design we solve this problem by defining two “branches” in the hierarchy:

  • IterableOps for collections whose type constructor takes one parameter,
  • MapOps for collections whose type constructor takes two parameters.

Here is a simplified version of IterableOps:

trait IterableOps[A, CC[_]] {
  def map[B](f: A => B): CC[B]
}

The CC type parameter stands for Collection type Constructor. Then, the List[A] concrete collection extends IterableOps[A, List] to set its correct self-type constructor.

Similarly, here is a simplified version of MapOps:

trait MapOps[K, V, CC[_, _]] extends IterableOps[(K, V), Iterable] {
  def map[L, W](f: ((K, V)) => (L, W)): CC[L, W]
}

And then the HashMap[K, V] concrete collection extends MapOps[K, V, HashMap] to set its correct self-type constructor. Note that MapOps extends IterableOps: consequently it inherits from its map method, which will be selected when the transformation function passed to map does not return a tuple.

Sorted collections

The third challenge is about sorted collections (like TreeSet and TreeMap, for instance). These collections define their order of iteration according to an ordering relationship for the type of their elements.

As a consequence, when you transform the type of the elements (e.g. by using the – now familiar! – map method), an implicit ordering instance for the new type of elements has to be available.

With CanBuildFrom, the solution relies (again) on the implicit resolution mechanism: the implicit CanBuildFrom[TreeSet[_], X, TreeSet[X]] instance is available for some type X only if an implicit Ordering[X] instance is also available.

In the new design we solve this problem by introducing a new branch in the hierarchy. This one defines transformation operations that require an ordering instance for the element type of the resulting collection:

trait SortedIterableOps[A, CC[_]] {
  def map[B : Ordering](f: A => B): CC[B]
}

However, as mentioned in the previous section, we need to also abstract over the kind of the type constructor of the concrete collections. Consequently we have in total four branches:

kind not sorted sorted
CC[_] IterableOps SortedIterableOps
CC[_, _] MapOps SortedMapOps

In summary, instead of having one map method that supports all the use cases described in this section and the previous ones, we specialized the hierarchy to have overloads of the map method, each one supporting a specific use case. The benefit is that the type signatures immediately tell you the story: you don’t have to have a look at the actual implicit resolution to know the result you will get from calling map.

Implicit builders

In the current collections, the fact that CanBuildFrom instances are available in the implicit scope is useful to implement, separately from the collections, generic operations that work with any collection type.

Examples of use cases are:

In the new design we are still experimenting with solutions to support these features. So far the decision is to not put implicit builders in the collections implementation. We might provide them as an optional dependency instead, but it seems that most of these use cases could be supported even without implicit builders: you could just use an existing collection instance and navigate through its companion object (providing the builder), or you could just use the companion object directly to get a builder.

breakOut escape hatch

As we have previously seen, in the current collections when we want to transform some collection into a new collection, we rely on an available implicit CanBuildFrom instance to get a builder for the target collection. The implicit search is driven by the type of the initial collection and the type of elements of the target collection. The available implicit instances have been designed to make sense in the most common cases.

However, sometimes this default behavior is not what you want. For instance, consider the following program:

val xs: List[Int] = 1 :: 2 :: 3 :: Nil
val xsWithSquares: Map[Int, Int] =
  xs.map(x => (x, x * x))

If you try to compile it you will get a compile error because the implicitly resolved builder produces a List[(Int, Int)] instead of the desired Map[Int, Int]. We could convert this List[(Int, Int)] into a Map[Int, Int] but that would be inefficient for large collections.

We can fix this issue by using the breakOut escape hatch:

val xs: List[Int] = 1 :: 2 :: 3 :: Nil
val xsWithSquares: Map[Int, Int] =
  xs.map(x => (x, x * x))(collection.breakOut)

breakOut selects a CanBuildFrom instance irrespective of the initial collection type. This requires the target type to be known, in this case via an explicit type ascription.

In the new design we have no direct equivalent of breakOut. The solution of the above example consists in using a View to avoid the construction of an intermediate collection:

val xs: List[Int] = 1 :: 2 :: 3 :: Nil
val xsWithSquares: Map[Int, Int] =
  xs.view.map(x => (x, x * x)).to(Map)

In practice, we expect that most usages of breakOut could be adapted to the new design by using a View followed by an explicit to call. However, this is an area that remains to explore.

Summary

In this article we have reviewed the features built on top of CanBuildFrom and explained the design decision we made for the new collections to support most of these features without CanBuildFrom.

Recently...

date-icon Friday, May 26, 2017 blog
It has been a little bit more than two months since Scala Native 0.1 has been released. What’s new in Scala Native? What should you...
date-icon Friday, May 19, 2017 blog
The Scala Center team is extremely happy to announce that Scastie is out of beta! That means anyone can use Scala in the browser over...
date-icon Tuesday, April 18, 2017 announcement
We are happy to announce three new Scala releases: 2.11.11 concludes the 2.11 series, 2.12.2 brings bug fixes and small improvements, and 2.13.0-M1 sets the...
date-icon
For more, visit our
News archive or Blog

Scala on Twitter