
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll look into the new parameter untupling capabilities introduced in Scala 3.
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)
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)
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.