1. Overview

Destructuring, a typical pattern in functional programming, offers practical benefits that are particularly useful in manipulating complex data structures. It allows us to bind attributes from complex data structures to named variables, thereby improving code readability and conciseness.

Destructuring isn’t a new concept; it first appeared in Lisp in the late 1970s. By using destructuring, we avoid repeated use of accessor functions or the prefix name to attributes in a language with dot notation.

Many programming languages have adopted variations of this feature, including Scala.

In this tutorial, we’ll discuss where and when to use destructuring in Scala and when to use the @ operator while destructoring data.

2. Quick Review of Destructuring in Scala 3

By breaking down data into its constituent components, we can streamline our code, reducing the need to prefix access to attributes in complex data structures or records. In Scala, we can use destructuring anywhere we explicitly bind values to names: variable assignments for comprehension and pattern matching.

2.1. Destructuring in Pattern Matching

Pattern matching is a powerful feature in Scala, enabling us to control branching based on the type and structure of the data. During pattern matching, we can bind names to attributes in the data we’re matching; this binding step is what we call destructuring and is optional. Not all matches need to destructure the data:

val response = Some("Found")

response match {
  case Some(value) => println(value)
  case None => println("Not Found")
}

2.2. Destructuring in Variable Assignments

Variable declarations are not limited to declaring and binding a single name. We can declare multiple names and bind them to different attributes of the input data structure; think of this as branchless pattern matching.

Hence, when dealing with ADTs, we must ensure the variable type matches the exact case since not all cases will be checked. For example, we can’t destructure an optional during the assignment; we should have a variable with the Some type:

val Some(response) = Some("Found")
val (x, y) = (1, 2)

case class Person(name: String, age: Int)
val person = Person("Alice", 30)

val Person(name, age) = person

val list = List(1, 2, 3)
val head :: tail = list

2.3. Destructuring in For-Comprehensions

For-comprehensions in Scala provide a syntactic sugar for sequencing computations, including iterating over collections. Destructuring within a for-comprehension allows for the direct unpacking of each element’s structure, enhancing the clarity of operations on complex data types:

val pairs = List((1, "one"), (2, "two"), (3, "three"))

for ((number, word) <- pairs) {
  println(s"Number $number is written as $word.")
}

This is similar to the variable assignment case, and as before, we can understand it as a branchless pattern matching.

3. When Destructuring Isn’t Enough

In some situations, we might need both the component attributes of a value and the composite object, but with the concrete type after the pattern match.

Let’s start by defining some classes and methods to illustrate this issue:

trait Notification
case class Email(subject: String, body: String, recipient: String)
  extends Notification
case class SMS(number: String, message: String) extends Notification

case class Processed[N <: Notification](notification: N, msg: String)

def processEmail(email: Email): Processed[Email] = Processed(
  email,
  s"Sent email to ${email.recipient} with subject: ${email.subject}"
)

def processSMS(sms: SMS): Processed[SMS] =
  Processed(sms, s"Sending SMS to ${sms.number}: ${sms.message}")

In the following test, we’ll process different notifications, but we’ll do some logging. In this case, if we bind the attributes, we’ll need to reconstruct the object:

"We" should "be able to pattern match without @" in {
  getRandomElement(notifications, random) match {
    case Email(subject, body, recipient) =>
      println(s"Logging Email to $recipient with subject $subject")
      // Reassemble and call the specific handler
      processEmail(
        Email(subject, body, recipient)
      ).msg startsWith ("Sent email to ")

    case SMS(number, message) =>
      println(s"Logging SMS to $number: $message")
      // Reassemble and call the specific handler
      processSMS(SMS(number, message)).msg startsWith ("Sending SMS to")
  }
}

The example above uses pattern matching to process two notification types. We start by logging the message using the attributes. Secondly, we call a processing method that requires an argument for the concrete notification type. Consequently, we can’t use the notification variable, forcing us to construct a new object of the correct type.

We could match on the type, but in that case, we’ll need to prefix the access to the object attributes:

"We" should "be able to pattern match without @" in {
  getRandomElement(notifications, random) match {
    case Email(subject, body, recipient) =>
      println(s"Logging Email to $recipient with subject $subject")
      // Reassemble and call the specific handler
      processEmail(
        Email(subject, body, recipient)
      ).msg startsWith ("Sent email to ")

    case SMS(number, message) =>
      println(s"Logging SMS to $number: $message")
      // Reassemble and call the specific handler
      processSMS(SMS(number, message)).msg startsWith ("Sending SMS to")
  }
}

But what if we could have our cake and eat it too?

4. Enhancing Scala Pattern Matching With the @ Operator

In Scala, we can use the @ operator anywhere we use destructuring, like pattern-matching:

"We" should "be able to pattern match with @" in {
  getRandomElement(notifications, random) match {
    case email @ Email(subject, _, recipient) =>
      println(s"Logging Email to $recipient with subject $subject")
      processEmail(email).msg startsWith ("Sent email to ")

    case sms @ SMS(number, message) =>
      println(s"Logging SMS to $number: $message")
      // Reassemble and call the specific handler
      processSMS(sms).msg startsWith ("Sending SMS to")
  }
}

The same applies to variable declarations and for-comprehensions:

"We" should "be able to use @ while declaring a variable" in {
  val email @ Email(subject, _, recipient) = getRandomElement(emails, random)
  println(s"Logging Email to $recipient with subject $subject")
  processEmail(email).msg startsWith ("Sent email to ")
}
"We" should "be able to use @ in for comprehensions" in {
  for (email @ Email(subject, _, recipient) <- emails) {
    println(s"Logging Email to $recipient with subject $subject")
    processEmail(email).msg startsWith ("Sent email to ")
  }
}

We need to prefix the pattern for which we need a complete value with a variable name followed by the @ operator. Furthermore, We can use the operator anywhere in the pattern, not only in the top pattern:

"We" should "be able to use @ in a subpattern" in {
  emails match {
    case _ :: (email2@Email(subject, _, recipient)) :: _ =>
      println(s"Logging 2nd Email to $recipient with subject $subject")
      processEmail(email2).msg startsWith ("Sent email to ")

    case _ =>
      println(s"Only one message")
  }
}

5. Conclusion

Throughout this article, we have explored the power of destructuring in Scala 3, showcasing its utility and flexibility across various programming scenarios. We also learned how to use the @ operator.

Destructuring simplifies complex data structure manipulation, allowing us to extract and work with individual properties directly. The @ operator eliminates the need to recreate destructured objects by binding the complete object to a variable while also binding the attributes.

These features, woven into Scala’s fabric, encourage an efficient and elegant programming style, making it our favorite tool to tackle the challenges of modern software development with finesse and clarity.

As always, the code companion to the article can be found over on GitHub.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments