Thursday, 15 February 2018

Kotlin: Data classes: Destructuring declarations

Kotling supports destructuring of object. By using destructuring declaration, we can define multiple variables in one line.

For ex
var (fName, lName, id) = employee
Above statement declares three variable fName, lName and id from employee object.

Find the below working application.

HelloWorld.kt

package com.sample.test

data class Employee(var firstName: String, var lastName: String, var id: String)

fun main(args: Array<String>) {
 var employee = Employee("Krishna", "Gurram", "12345")

 var (fName, lName, id) = employee

 println("fName : $fName")
 println("lName : $lName")
 println("id : $id")

}

Output

fName : Krishna
lName : Gurram
id : 12345


Previous                                                 Next                                                 Home

No comments:

Post a Comment