Destructuring Assignment, Nested destructuring — Part 2/2
In the first article, Destructuring assignment — Part 1/ 2, I compared how destructuring assignment works in Kotlin and Scala. How about nested object destructuring? You mighty ask😃.
Consider the following Kotlin code:
data class Course(val courseId: String, val name: String)
data class Student(val name: String, val course: Course)
How would you destruct an object of Student
class? Doing this produces compile time errors.
val course = Course(name = "Kotlin course", courseId = "KT")
// Create an object of Student class
val student = Student(name = "Kotlin student", course = course)
// Destructuring assignment
// This produces compile time errors
val (studentName, (courseId, courseName)) = student
How about Scala?
Consider the following:
case class Course( val courseId: String, val name: String)
case class Student(val name: String, val course: Course )
val course = Course(courseId = "SC", name = "Scala course")
// Create an object of student class
val student = Student(name= "Scala student", course = course)
Then you can deconstruct student object like this:
val Student (studentNme: String, Course(courseId, courseNme: String)) = student
And use individual variables independently:
println(s"Student name => $studentName")
println(s"Course Name => $courseName")
Case classes in Scala are assumed to be immutable. The Scala compiler generates helpful methods like apply
, unapply
, copy
, equals
and hashCode
for you. unapply
method is used in deconstruction of respective objects.
This line of code:
val Student (studentName: String, Course(_, courseName: String)) = student
is compiled to:
val studentName = Student.unapply(student).get._1
val courseId = Course.unapply(course).get._1
val courseName = Course.unapply(course).get._2
Kotlin on the other hand uses componentN
function to unpack values from an object.
To extra values from an object of the Student
class, you’d do this
data class Student(val name: String, val course: Course)
val course = Course(name = "Kotlin course", courseId = "KT")
// Create an object of Student class
val student = Student(name = "Kotlin student", course = course)
// Extract values from the student object
val (studentName, courseDestruct) = student
// Extract values from the course object
val (courseId, courseName) = courseDestruct
When you look at the byte code for the Student
class, you’ll see something like this:
Then this code:
val (studentName, courseDestruct) = student
is compiled to:
val studentName = student.component1()
val courseDestruct = student.component2()
Conclusion
Kotlin and Scala share fundamental similarities in nested destructuring. There are subtle variations too. In syntax and pattern matching capabilities. We’ll explore pattern matching in a future article 😃. Selecting the appropriate language depends on your specific project requirements and preferences. If you value a concise and familiar destructuring syntax, Kotlin might be a good choice. On the other hand, if you need more powerful pattern matching functionalities, Scala could be a more suitable option.
Happy Coding! 🙌🏽