Introduction to Kotlin | Application of Kotlin Part 3
Now, one other thing that you have is also the copy class.
So here, you can see that I have a data class contacts and then I have a list of type email, which is in turn is a data class.
And if you do a copy contacts, this in turn is actually going to do a deep copy. So it’s going to copy everything for you.
So you’re going to get all of those different properties. And you can, in fact, overwrite this. You can say, I want the company name to be when I copy, I want, for example, the company name to be something else. And then it will default the other ones to whatever the value is.
So you can override the values as you’re doing the copy . We use data classes, as you know. We like data classes. Initially, we used to have two pools.
And we got rid of them, because one of the key aspects of Kotlin is to write maintainable code. Now, we can’t guarantee that, because we are developers.
And we can take any language and make it completely unreadable. And as Kevlin Henney recently said, there’s a difference.
Every code is readable. The bigger issue is whether it’s comprehensible, which is where we basically struggle. But we do have pairs and triples, So, for instance, let’s say that I have a function that returns pairs. So just basically, I can just return pair of type string string. And we also have triples.
So if you go beyond triple, then we say for you to use a data class. So here, I could just do pair something else.
Now, if I call this function, how do I access those properties? Well, the first one is with first. And guess what the second one is going to be?
There you go. You’re really interactive, aren’t you? Anyway, so the second one is going to be second. Now, that kind of sucks, because, hey, we’re talking about readability, comprehensibility. And what is first? What is second?
So you have destructuring of variables in Kotlin. So you can do, for example, name and email, and then do this as return pairs.
And now I can access those in a much more nicer way, giving me context and meaning of what exactly it is that I’m accessing.
So I could do now print email. And this destructuring you get when you’re working with data classes, you get when you’re working with pairs, you get when you work in with lambdas.
And notice here that it’s saying, well, you’re not actually using name. So that’s a hint that the compiler is giving you, and the IDE is surfacing for you.
What you can do is also use the underscore, and say, anything that I don’t use, I’ll just use underscore for, and then just get rid of all of those hints.
But more importantly, tell the developer that’s reading this code that this value is not important. And as I said, this destructuring can work with anything.
So, for instance, we’ve got contacts or our email class. So I can say, this is the name, name1, and this is the email1, and then do exactly the same for email.
So I could say something and something. So it’s not only with pairs. It also works with data classes. And it’s basically following the convention of the first parameter is the first position, the second parameter is the second position, so on and so forth.
Where else do we try and improve on the boilerplate code that you write?