Since Google officially announced Kotlin as the language of choice for Android development, kotlin has become something of a hurdle for older Javaers. If you had asked me six months ago what a Kotlin was, I would have said, right

Oh, kotlin, just like Java, it compiles to bytecode and runs on the JVM. Seems to have a lot of cool features that make it easy to use, right?

If you ask me again, I’ll answer you lightly

Does knowing how to spell kotlin count?

It’s not that I don’t want to learn, there are endless bugs to change every day, there are endless requirements to do, there are mountains of 5 or 6 years old code in the project, and I can’t do it!!

Okay, I put my cards on the table. I just don’t want to learn. After all, do not know monkey years horse months can be used, learn to forget, there is that time is not good sister or the game is not fragrant?

All the changes happened half a year ago. When the leader and mentor were staring at you with death stare and charming smile, I could only put down my happy hands and walk to kotlin’s world with fear and trembling.

There are so many bits and pieces that I’m sure your handsome brothers won’t read, so I’ll just pick one or two representative ones

The method is first-class citizenship

We often hear some of the bigwigs around us arguing passionately about what is best in the world and what is the future of functional programming. Kotlin is a combination of object-oriented and functional programming. Object-oriented, of course, because it is Java compatible, and functional programming is the secret to its higher level. In Kotlin’s world, methods (functions) are first-class citizens.

First-class citizenship is the most important thing.

If we think back to the Java world, what is the most important thing? I think it’s a property of an object. In the development process we use the most is the property, method is more we deal with the property of a process. In the Java world, methods sit there forever, waiting for us to pass them a value and then return a result.

Not Kotlin. In Kotlin’s world, methods can be transferred as freely as properties, and fly as freely as possible. A method is just a process of processing data. I can pass a method to a method parameter, and the result can be a data or a method.

If that doesn’t sound human, write it in code

Class Bob {fun makeHouse(money: Int): String {return "I can make a car"}} Class Ace {fun makeHouse(monkey: Int): String {return "I can build a house"} // I can steal fun woHuiTou(monkey: Int, makeCar: (Int) -> String): String { return makeCar.invoke(monkey) } }Copy the code

Let’s say there are two people, one named Bob and one named Ace. Bob is honest and honest. He can build cars. Ace can build houses. One day Ace’s mind was out of balance. “You say I can build a house, but I can’t even build a car.”

So Ace managed to steal Bob’s car-building skills. So let’s run it

fun main() {
    val bob = Bob()

    val ace = Ace()
    ace.woHuiTou(100, bob::makeCar)
}
Copy the code

Methods are first citizen meaning that methods can be passed as references

This is also called a higher-order function. Bob ::makeCar indicates that Bob’s car-building skills are passed to makeCar, the parameter to the woHuiTou method. MakeCar then calls the Invoke method to execute.

Method type and passing

The idea of methods as parameters is kind of mysterious, kind of amazing. It’s really easy to just figure out what the type of method is, right

Methods can also be passed like properties. Properties must have a type. What is the type of a method?

Don’t worry, this question can be exchanged for another

In Java, if you were asked to pass a method, what would you do?

The simplest, of course, is to put the method in an object, pass the instance of the object, and call the object method when it needs to be executed

Brother good cow force!! Kotlin did the same thing! Kotlin wraps the method we’re passing around in a Function object, which is the type of the method.

Function2 indicates that two arguments are passed in. P1, P2 is the incoming type, and R is the returned type

As for method passing above we already know, rely on method reference. The two colons :: are kotlin’s syntax, and Bob ::makeCar represents a reference to a method on an instance object.

That’s what higher-order functions, functional, combinatorial programming is all about. It’s really that simple. Of course, this is what differentiates Kotlin from Java

Of course, Kotlin has other amazing advantages, such as extension functions and coroutines, which will be shared with your handsome brothers next time

I am the wood

A worker struggling to make his way up in the Internet world

Try to live, try to move forward

If you feel good, please follow me and I will try my best not to disappoint you