One of the things in Kotlin is extension functions, So what I can essentially do is do fun string Krakow.
And then what that will do is essentially print line Krakow is great, So now I can say Poland and Krakow are great, So I’ve just added a new function to Krakow are great, It’s only done Krakow. Why?
Because I’ve written Krakow are great. Now what I could do is actually reference. This is essentially what it’s doing is adding an extension function to the type string. And I can extend any type in Kotlin or Java.
So you’re thinking, well, why would I want to do this? Again, to make code expressive. Now I normally wouldn’t add a function in my codebase called Krakow.
We can do something like two camel case with inverted colons German style, I could do something like that, because you know that in Germany, they do.
They invert things. So this gives you, again, expressivity. I don’t have to take a string class, inherit from it, add a new method to be able to just add one more function. This is where the beauty of this extension functions are.
And if you actually look at the standard library, which we’ll see in a moment, most of the functionality you get are in fact extension functions over generic types, meaning that you get a whole bunch of functionality independently of the type of things you use, whether it’s string, whether it’s collection.
And we’ll see that when we take a look at lambdas. Now talking about lambdas, lambda in Java. I have a lambdas in Java.
If I want to declare something in Java that is a higher order function, I say, well, I can do it in two ways. I can define an interface that takes two parameters.
Or I can do something like a by consumer, or a by function, or a whatever, and then have all of these different types, and then pass in the parameters that I need.
And then the IDE will say to me, OK, this is a lambda. You don’t have to implement a route handler. You can just pass this in as a lambda, just like I’m using, like the using listener in Java. Here I’m creating a new instance.
But I can do Alt-Enter and replace that with lambda. You can do pretty much the same thing in Kotlin, except you don’t have to have the explicit type declaration. You can just pass in lambda like that.
And you’ll see that this is omitting the brackets, which we’ll see in a moment. But how do I work with lambdas in Kotlin, and how do I declare higher order functions?
Essentially, we’re using always the same syntax. I don’t have to know about by function or things like that.
I just say int, int returns int, meaning that this is a function that takes two integers, and returns an integer.
And then I’m invoking that function. Here is a function that takes no parameters, and returns void, So technically, it’s not a function. It’s a method.
But anyway, so always following the same syntax, always. And now, it clearly says to me, oh, this is a lambda. I can pass in a lambda.
And when you want to invoke it, you do higher order, and you actually pass in the lambda, which we use this syntax, which is the same if the last parameter to have a function is another function, then I can omit the curly braces.
And then what I can do is make this multi-line, which will make it a little bit nicer.