My favorite new features of Scala 2.13 standard library

Linas Medžiūnas
Jul 17, 2019 · 4 min read
Image for post
Image for post

A new release of Scala programming language, 2.13, has been announced recently. It is time to look at what improvements it has brought to us. In this post I will focus on the changes in the standard library, so this can be considered a follow up to my previous post, The little gems of Scala standard library. Many of the features described here are not mentioned in the official release notes (where you can find a broad description of general language, collection and compiler improvements).

1. scala.util.Using

Example:

import scala.util.Using
import java.io.{FileReader, FileWriter}
Using.resources(
new FileReader("input.txt"),
new FileWriter("output.txt")) { (reader, writer) =>
???
}

Note: if you are not moving to Scala 2.13 just yet, but would like to have a decent implementation of try-with-resource in Scala, I suggest reading this useful post by my colleague.

2. minOption / maxOption (also minByOption / maxByOption)

3. scala.collection.Stepper

Also, Stepper provides interoperability with some of the modern features of Java introduced in version 8, such as java.util.Stream, java.util.PrimitiveIterator and java.util.Spliterator.

You can get an instance of Stepper based on any Scala collection, simply by calling a method .stepper on it.

4. sizeIs

Example that validates the size of linked list, without fully traversing it:

val xs = List.fill(5000)(scala.util.Random.nextInt)if (xs.sizeIs > 100) { // traverses no more than 101 element
throw new IllegalArgumentException("Too many numbers!")
}

5. mapInPlace

6. minAfter / maxBefore

7. scala.collection.mutable.ArrayDeque

8. Deprecated Map.filterKeys and Map.mapValues

Many of the features mentioned in this post were not covered by the official release notes. Most likely there still is something more that I have missed. If you discover more interesting “hidden” features of Scala 2.13, please share them in the comments!

P. S. Release notes of Scala 2.13 have been updated to include the changes mentioned in this post. I was really happy to hear that, even though it made some of my statements above no longer accurate 😌.

Wix Engineering

Architecture, scaling, mobile and web development…

Linas Medžiūnas

Written by

Software engineer at Chronosphere.io; unretired competitive programmer; 2x IOI gold medal winner; curious about Quantum algorithms.

Wix Engineering

Architecture, scaling, mobile and web development, management and more, written by our very own Wix engineers. https://www.wix.engineering/

Linas Medžiūnas

Written by

Software engineer at Chronosphere.io; unretired competitive programmer; 2x IOI gold medal winner; curious about Quantum algorithms.

Wix Engineering

Architecture, scaling, mobile and web development, management and more, written by our very own Wix engineers. https://www.wix.engineering/

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store