Editors Top

We’re looking for a new Scala technical editor to help review new articles for the site.

1. Overview

In this tutorial, we’ll look into the new parameter untupling capabilities introduced in Scala 3.

2. Parameter Untupling

Parameter untupling is one of the many changes introduced in Scala 3. This new language feature allows us to work with tuples with less boilerplate. In previous Scala 2 versions, if we wanted to iterate on a list of pairs, we’d do it with pattern-matching decomposition by using a case statement:

val lst = List((1,2), (3,4)) 

lst.map { case (x, y) => x + y }

> val res0: List[Int] = List(3, 7)

3. Parameter Untupling in Scala 3

While the previous code snippet works just fine, it was also a source of confusion for many programmers. New Scala developers would think the syntax looks like partial functions, suggesting that for some inputs the function would fail. Scala 3 addresses this issue by introducing a shorter and clearer alternative, using parameter untupling:

lst.map { (x, y) => x + y }

> val res0: List[Int] = List(3, 7)

In this particular case we could simplify a bit further:

lst.map(_ + _)

> val res0: List[Int] = List(3, 7)

4. Conclusion

In this short article, we saw one of the many new features introduced in Scala 3: parameter untupling. This language feature allows for a clear syntax when working with tuples.

Comments are closed on this article!