Kotlin points out that

This article is my Kotlin in the process of sorting out some knowledge points, if there is any mistake, welcome to correct

Operator overloading

Operator operator operator operator operator operator operator operator operator operator operator operator operator

class OBj{
    operator fun plus(obj: Obj): Obj{
     // Handle adding logic}}// Call the following method
val obj1 = Obj()
val obj2 = Obj()
val obj3 = obj1 + obj2 
Copy the code
Syntactic sugar expressions and the actual function called comparison table
Syntactic sugar expression Actually calling the function
a + b a.plus(b)
a – b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b a.rem(b)
a++ a.inc()
a– a.dec()
+a a.unaryPlus()
– a a.unaryMinus()
! a a.not()
a == b a.equals(b)
a > b a.compareTo(b)
a < b
a >= b
a <= b
a.. b a.rangeTo(b)
a[b] a.get(b)
a[b] = c a.set(b, c)
a in b b.contains(a)

Taintaines (a), taintaines (a), taintaines (A), taintaines (A), taintaines (A), taintaines (A)

Examples of extending function + operator overloading

Let’s look at an example. Now we’re going to use the extension function + operator overload to print a string a specified number of times, like STR * n:

operator fun String.tines(n : Int) : String {
    val builder = StringBuilder()
    repeat(n) {
        builder.append(this)}return builder.toString
}
Copy the code

The operator keyword is essential, the function name is tines by referring to the table above, and since we are adding an extension function to the String, we prefix the method name with String, and we repeat the String n times with repeat().

Strings now have the ability to multiply numbers, for example by executing the following code:

val str = "abc" * 3
println(str)

// The final print is: abcabcabc
Copy the code

The String class in Kotlin already provides a repeat() function, so we can simplify the above function as follows:

operator fun String.tines(n : Int) = repeat(n)
Copy the code

Higher order functions

1. Define higher-order functions

Definition: A function is called a higher-order function if it takes another function as an argument, or if the type of the return value is another function.

Define a function type:

(String, Int) - >Unit
Copy the code

-> The left part is used to declare what arguments the function accepts. Multiple arguments are separated by commas. If no arguments are accepted, write a pair of empty parentheses. If there is no return value, use Unit, which is roughly equivalent to void

Define a higher-order function:

fun example(func: (String.Int) - >Unit){
    func("hello".123)}Copy the code

Purpose: Higher-order functions allow function type arguments to determine the execution logic of the function. Even with a higher-order function, its execution logic and return result can be completely different if different function type arguments are passed in. Let’s look at an example:

fun StringBuilder.build(block: StringBuilder. () - >Unit): StringBuilder{
    block()
    return this
} 
Copy the code

We define an extension function build for StringBuilder that takes a function class parameter. And if the function type is preceded by the syntax of ClassName., it indicates the class in which the function type is defined. The function type is defined in the StringBuilder class. What this does is that when we call build, the Lambda expression passed in automatically has the StringBuilder context, similar to apply, except that apply applies to all classes. This only applies to the StringBuilder class. Usage:

val result = StringBuilder.build{
    append("Start here \n")
    append("str1 str2 \n")
    append("End here")}Copy the code

2. Inline functions

Inline functions are primarily designed to address the runtime overhead associated with running Lambda expressions. Inline functions use the inline keyword to define higher-order functions as follows:

inline fun num1AndNum2(num1:Int,num2:Int,operation: (Int.Int) - >Int) : Int{
    val result = operation(num1, num2)
    return result
}
Copy the code

Noinline only wants to inline one of the Lambda expressions:

inline fun num1AndNum2(block1 : () -> Unit.noinline block2 : () -> Unit){}Copy the code

Q: Why does Kotlin provide a noinline keyword to exclude inlining? A: The function type argument of an inline function is code replaced at compile time, so it has no real parameter attribute. A non-inline function type parameter can be passed freely to any other function because it is a real parameter, whereas an inline function type parameter is only allowed to be passed to another inline function. (Note: There is another important difference between an inlined function and a non-inlined function: the return keyword can be used to return a function from a Lambda expression referenced by an inlined function, whereas a non-inlined function can only return partially.)

Kotlin: What are you doing

Resources: First Line of Code (Third Edition)