Kotlin

Introduction to Kotlin | Application of Kotlin Part 8

Introduction to Kotlin | Application of Kotlin Part 8

I can create a function called using, for instance, object, which is closable, and then have a  body, that basically, what it’s doing is going to invoke the body.

And then I’ll do finally object close. And then what I could do imagine that I have a val closable thing here, whatever.

We’ll close it, whatever. Imagine that that works. And then I could do using closable, and then not have to worry about that resource being freed.

Using as a statement that we have in C# and you have similar with resources, but essentially giving you that fluidity of feeling as if you are adding functionality to the language in a way that is very expressive and easy to read and understand.

And all of these functions, as I mentioned, we have. When working with functions, all of these things are part of the standard library. So, by the way, this is how you create a very nice easily, you create a list of numbers.

And Kotlin is one to 100. And then on the numbers, I can do things like filter. It is less than 100. So it is following the same syntax as Groovy, saying that if the function that I’m passing in is a single parameter, I don’t have to explicitly name that parameter.

So instead of doing something like x, and then x is less than 100, I can actually convert that into using implicit.

Kotlin
Kotlin

And then I can chain these. Map to it div 100, or what have you, So these functions, as I mentioned, they’re basically part of the standard library. You can see that they’re extension functions on interval of t.

So I have all of this functionality for all the different types of collections that I want to use. And Kotlin doesn’t provide right now its own collections.

It basically provides a set of interfaces on top of Java’s collections, by default, moving towards immutable.

So when I do, for instance, val customers equals list of, and then a customer Kotlin, that is going to create an immutable customer for me in Kotlin.

And I can add a second one, customer Kotlin, and then get whatever I want here. One other thing, difference with Java, is that in Java, you would have the Java streams over here, streams in Java.

So the equivalent of what I did is 1 to 100 is in stream, in stream range, 1 to 100. The difference is that this in Kotlin, all of these lists are equally evaluated. If you want to do lazy evaluation, then what you do is you just write ask sequence.

And you can do that to any collection. And if you want to do it to something a little bit more complex than a collection, what you can do is use the top level function, generate sequence, and then here, pass in a C.

And then on this, basically generate the code that you want to, and then on that, map it to, or filter it is less than 100 and then for each, and then here, print line it.

Chain those together, One other thing that we’ve tried to again, focusing on it is just more compact, more concise, easier to read.

Another thing that we have, which is nice, is the return types. The return types are a way of implementing algebraic data types, which is useful, where you essentially say that a type, a function, for instance, is returning one type or another.

A type can be one type or another. The way that we represent that in Kotlin is essentially through a hierarchy of classes. But then what we do is add the sealed modifier, which essentially means that this hierarchy is closed, meaning that nobody else can ever inherit from response And these are inside.

I can actually take them outside, and then have something like failure, success, and response, and then get rid of all of these things. And notice– I don’t know if you see it there, but this has gone green.

This has gone green because that’s another thing that we have in Kotlin, which is smart class, which essentially means save me having to type things over and over again. And instead of casting this to type success.

I can essentially just assume that it is success, because the compiler has done that for me. So these are some of the things that Kotlin provides you in order to be able to work more concisely, and more fluently, and more rapidly, and more fun.

March 22, 2019 Ravindra Datir

Leave a Reply

Your email address will not be published. Required fields are marked *