Destructuring Declarations in Kotlin

What all we can use destructure?

you can destructure data class and Collections, you can also use them in other components such as in loops, lambdas, and return types.

How to Blow Your Code with Destructuring?

Under the Hood!

operator fun <T> Array<out T>.component1(): T
operator fun ByteArray.component1(): Byte
operator fun ShortArray.component1(): Short
operator fun IntArray.component1(): Int
operator fun LongArray.component1(): Long
operator fun FloatArray.component1(): Float
operator fun DoubleArray.component1(): Double
operator fun BooleanArray.component1(): Boolean
operator fun CharArray.component1(): Char
operator fun <T> List<T>.component1(): T

Happy ending? #Spoiler nope!

Possible Deal Breaker!

Destructuring Use-Cases

  • Using below trick you can enforce positionally destructing so even if contact breaks due to rearranging of members doesn't affect the destruct code.

The End.

Congratulation you made it till the end!. if you found anything interesting feel free to share in response. Happy Coding!

Coding enthusiast! love Android #kotlinAlltheWay, want to explore all opportunity around it! CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store