Learning by Example

Kotlin: Destructuring Declarations

Part 2: Returning more than one value from a method

Thomas Sunderland
The Startup
Published in
3 min readAug 4, 2019

--

One Function, Multiple Outputs

In my first article on destructuring declarations I demonstrated how you can take advantage of this Kotlin language construct by providing an example usage from the context of working with locations in Android.

Taking the Android Location class, we extended it with custom componentN() functions enabling us to more easily work with just the latitude and longitude properties of a given Location instance as needed.

One benefit of destructuring declarations that I did not touch on, however, is how you can also use them to, in effect, return more than one value from a function. So let’s take a look now at how we can accomplish this.

Let’s say that we have a Customer class and an Address class defined as follows.

As you can see our basic representation of a Customer is very simple and has just a first and last name and an address. Note that we purposely did not define the Customer class…

--

--