DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Learning Kotlin: Destructuring

Learning Kotlin: Destructuring

Want to learn yet another feature of Kotlin? Check out this post where we explore how to perform the destructuring of objects in Kotlin.

Robert Maclean user avatar by
Robert Maclean
·
Sep. 28, 18 · Tutorial
Like (11)
Save
Tweet
Share
10.73K Views

Join the DZone community and get the full member experience.

Join For Free

Learning a new language seems to be an experience that you undertake to:

  1. Change jobs
  2. Because your boss made you do it
  3. Because you are a "nerd"

The thing I forget each time I learn a new language is that the act of learning a new language helps me become a better software developer in my own primary language (the secret fourth option). Going through Kotlin has been a similar experience, and nothing jumped out more than object destructuring.

The simple use for object destructuring is to be able to do so in a single line and assign multiple variables from an object. Let's look at this example:

data class Person(val firstName: String, val surname: String, val age: Int)

fun name(person: Person) {
    println("Hi ${person.firstName}")
}

fun name2(person: Person) {
    println("Hi ${person.firstName} ${person.surname}")
}

fun main(args:Array<String>) {
    val frank = Person("Frank", "Miller", 61)
    val alan = Person("Alan", "Moore", 64)

    name(frank)
    name2(frank)
    name(alan)
    name2(alan)
}


In each of the name and name2, I am working with the Person object, but all I want are the names. I never care about the age of the people.

We could add a function now, which pulls out just the strings we want and modify everything else to work with JUST the data it needs:

data class Person(val firstName: String, val surname: String, val age: Int)

fun name(firstName: String) {
    println("Hi $firstName")
}

fun name2(firstName: String, surname: String) {
    println("Hi $firstName $surname")
}

fun print(person: Person) {
    val (firstName, surname) = person
    name(firstName)
    name2(firstName, surname)
}

fun main(args:Array<String>) {
     val frank = Person("Frank", "Miller", 61)
     val alan = Person("Alan", "Moore", 64)

    print(frank)
    print(alan)
}


Line 12 is where the magic happens — that is the Object Destructuring. Rather than having two lines where we assign a variable to firstName and surname, we can assign them both in one line so long as they are wrapped in parenthesis and match the names of the properties of the object.

So, why is this useful for other languages? This is because, in JavaScript, you have the same thing! The only difference is { and } rather than parenthesis, and since learning it in Kotlin, I've found that I use it in my main more, too.

Happy learning!

Kotlin (programming language)

Published at DZone with permission of Robert Maclean, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What To Know Before Implementing IIoT
  • How To Perform Local Website Testing Using Selenium And Java
  • What Is the Temporal Dead Zone In JavaScript?
  • Best Practices for Writing Clean and Maintainable Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: