1. What infix does

Infix indicates that functions can use infix notation. Infix notation is ignored. And () and three conditions must be met

  • Must be a member function or extension function
  • There must be only one parameter
  • Its parameters cannot have variable parameters and cannot have default values

Precautions for use:

  • Level is lower than the firstArithmetic operator ,Type conversion ,rangeToThe operator
  • First level higher than&&.||

Infix allows syntax to be written more like English, human language. Here’s an example to visualize it

Shifts this value left by the bitCount number of bits. Note that only the five lowest-order bits of the bitCount are used as the shift distance. The shift distance actually used is therefore always in the range 0.. 31. public infix fun shl(bitCount: Int): IntCopy the code

Use: 1 SHL 2 can also be 1. SHL (2) is equivalent

2. Inline functions

Context for the occurrence of inline functions:

Higher-order functions cause run-time efficiency problems, and each function is an object, so it consumes more memory.Copy the code

Advantages and disadvantages of inline functions:

However, inline should be used properly. It is recommended to modify functions that are not logically complex and do not have recursion. Otherwise, performance will be affected.Copy the code
Inline functions Inline fun <T> lock(lock: lock, body: ()->T): T {.... }Copy the code

3. Noinline disables inline

When using inline functions, if you want an argument to be inline, you can use noinline to limit it

Inline fun test(a:()->Unit, noinline b:()-> b)Copy the code

Labmda expressions that can be inlined can only be called inside an inlined function or passed as arguments that can be collectable.

A noinline – modified function can be assigned to a variable.

4.crossinline

Crossinline is used to disallow non-local return of labeled lambda expressions.

Nonlocal return:

In Kotlin, we can only exit with a normal unqualified return for named or anonymous functions. This means that to exit a lambda expression, we must use a tag, and naked returns are prohibited inside lambda expressions because lambda expressions cannot jump out of the containing function

Return fun hasZeros(ints: List<Int>): Boolean {ints. ForEach {if (it == 0) return true} return false}Copy the code

Partial return:

inline fun inlined(block: () -> Unit) { println("hi!" } fun foo() {inlined {return // OK: the lambda expression is lined}} fun main() {foo()}Copy the code
Inline fun f(crossinline body: ()->Unit) {val f = object: Runnable {override fun run() = body()} }Copy the code

4. Refied specified type parameters

Fefied: this is what generic type inference is for and needs to be inline with it.

Ordinary functions (not marked inline) cannot have externalized arguments. A type that does not have a runtime representation (such as an unmaterialized type parameter or a fictitious type like Nothing) cannot be used as an argument to a materialized type parameter.

Practical examples:

inline fun <reified T:Activity> Activity.startKtxActivity(context:Context){ startActivity(Intent(context,T::class.java)) StartKtxActivity <MainActivity>(this)Copy the code