The Programmers Guide To Kotlin - Destructuring
Written by Mike James   
Monday, 14 September 2020
Article Index
The Programmers Guide To Kotlin - Destructuring
The Spread Operator

The Spread Operator

There is one special type of destructuring designed to make it easier to use varargs. If you pass parameters to a vararg one by one it builds an array for you to hold them.

What if you already have the arguments in an array?

This is where the spread operator * comes in. It allows you to pass an array directly to the varargs as an array. You can think of it as unpacking the array into separate parameters which is why it can be regarded as a destructuring operator.

For example:

val a=arrayOf(1,2,3)
val list =asList(*a)

The asList method expects a vararg of the individual elements that will be used to create the list. The spread operator passes the array as if it was a set of individual parameters.

Summary

  • Kotlin doesn’t provide structs or any value alternative to classes, but it does provide a data class which has data properties and a set of methods to work with them.

  • Equality is a difficult thing to define in an object-oriented world. There are two basic equality operators == for equality of reference and === for structural or content equality.

  • If you want equality to be correctly interpreted for your custom classes you need to implement your own equals method. This can either perform a shallow or a deep comparison.

  • Arrays have a referential definition of equals, but you can also use contentEquals for a shallow structural equals and contentDeepEquals for a deep structural equals.

  • Data classes, List, Map and Set have a generated shallow equals.

  • Enums allow you to construct ordinal data representations that map names to integers. An enum behaves like a static class that has properties that are instances of the same type.

  • An enum can have properties and methods but these are shared between all instances of the type.

  • Sealed classes provide an alternative to enum but you have to do more work to implement similar behavior. They work like a set of derived classes that form a known set. The compiler will check that you have included them all in a when expression.

  • Delegation is an alternative to inheritance and you can automatically delegate property implementation to a specific object that implements a delegated interface.

  • Destructuring is a simple mechanism for unpacking the data contained in a structure into individual variables.

  • The spread operator * allows you to pass an array to a vararg parameter.

This article is an extract from: 

Programmer's Guide To Kotlin

Now Available as a Print Book

cover

You can buy it from: Amazon

Contents

Some Chapters Already Available On The Web

  1. What makes Kotlin Special (Book Only)
  2. The Basics: Variables, Primitive Types and Functions
  3. Control
  4. Strings and Arrays
  5. The Class & The Object
  6. Inheritance
  7. The Type Hierarchy 
            Extract  Type and its problems
            Extract  Smart Casts
  8. Generics
            Extract Basic Generics
            Extract Covariance & Contravariance
  9. Collections, Iterators, Sequences & Ranges
            Extract     Collections
            Extract Iterators & Sequences
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
            Extract    Annoymous and Lambda Functions
            ExtractInline Functions
  12. Data classes, enums and destructuring
            Extract  Data Classes
            Extract      Enums & Sealed Classes 
            Extract 
         Delegated Properties 
            Extract      Destructuring  **NEW!**
  13. Exceptions, Annotations & Reflection
            Extract Exceptions
            Extract Annotation & Reflection 
  14. Working with Java
            Extract Using Swing  

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, Facebook or Linkedin.

Banner


Learn About Serverless with The Linux Foundation on edX
11/09/2020

A new free training course that explains serverless computing and provides first-hand experience in building and deploying code directly to a Kubernetes cluster has just launched on the edX platf [ ... ]



Microsoft Jumps on Wonder Woman 1984 Bandwagon
04/09/2020

Wonder Woman 1984 is this Fall's most anticipated film and Microsoft is taking advantage of the hype and razzamatazz to gain attention for themed learn-to-code activities.


More News

graphics

 



 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962536>

<ASIN:1871962544>



Last Updated ( Monday, 14 September 2020 )