preface

In Kotlin, there are some built-in functions that extend and make coding easier for developers, making it much more efficient. Today, I will mainly explain:

  • Let the function
  • The braking function
  • With the function
  • The run function
  • The apply function

Basics: Lambda is used in interface callbacks

Lambda functions can be used in Kotlin to simplify some unnecessary nested interface callback methods

Note: Only a single abstract method callback is supported, not multiple callback methods.

SetEventListener (new ExamEventListener(){public void onSuccess(Data Data){//... }}); // Kotlin interface callback with the same effect (without lambda expression) mvar.seteventListener (object: ExamEventListener{ public void onSuccess(Data data){ // ... }}); // Kotlin interface callback (using lambda expressions, leaving only arguments) mvar. setEventListener({data: data -> //... }) // Continue simplification // Simplification 1: With kotlin's intelligent type derivation, ignore the data type mvar.seteventListener ({data -> //... }) // Simplification 2: Ignore mvar.seteventListener ({//... }) // Simplify 3: If the last argument to the setEventListener function is a function, the implementation of the parenthesis can be raised outside the parenthesis mvar.seteventListener (){//... } // Simplify 3: if the setEventListener function has only one argument & is not used, omit the parentheses mvar.seteventListener {//... }Copy the code

Next, I’ll take a look at some of the useful built-in functions that Kotlin provides for extending and facilitating developer coding: let, Also, with, run, and apply.


1. Let the function

1.1 introduction

1.2 Usage

Let {it.todo()} // Function 1: use it instead of object to access its public attribute & method Object.let {it.todo()} .todo()}.todo()}.todo() {.todo()Copy the code

1.3 Example

// Use Java if(mVar! = null ){ mVar.function1(); mVar.function2(); mVar.function3(); } // use kotlin (no let function) mVar? .function1() mVar? .function2() mVar? .function3() // use kotlin (use let function) // to facilitate the same null processing & determine the scope of the mVar variable mVar? .let { it.function1() it.function2() it.function3() }Copy the code

2. The braking function

2.1 Functions & Application Scenarios

Similar to let, except that the return value is:

  • Let function: return value = last line/return expression
  • Also function: Returns the value = itself of the object passed in

2.2 Example

// let function var result = mvar.let {it.function1() it.function2() it.function3() 999 Var result = mvar.also {it.function1() it.function2() it.function3() 999Copy the code

3. With the function

3.1 role

When calling multiple methods/attributes of the same object, you can directly invoke the method name/attribute without the object name duplication

3.2 Application Scenarios

Multiple methods/properties of the same object need to be called

3.3 Usage

with(object){ // ... } // Return value = the last line of the function block/return expressionCopy the code

3.4 Example

// kotlin val people = people (" Carson ", 25) with(people) {println("my name is $name, I am $age years old") } // Java User peole = new People("carson", 25); String var1 = "my name is " + peole.name + ", I am " + peole.age + " years old"; System.out.println(var1);Copy the code

4. Run function

4.1 Functions and Application Scenarios

Combined with the functions of let and with, namely:

  1. When calling multiple methods/attributes of the same object, you can directly invoke the method name/attribute without the object name duplication
  2. Defines a variable in a particular scope
  3. Do a unified short processing

4.2 Usage

object.run{ // ... } // Return value = the last line of the function block/return expressionCopy the code

4.3 Example

// kotlin val people = people (" Carson ", 25) people? .run{ println("my name is $name, I am $age years old") } // Java User peole = new People("carson", 25); String var1 = "my name is " + peole.name + ", I am " + peole.age + " years old"; System.out.println(var1);Copy the code

5. The apply function

5.1 Functions & Application Scenarios

This is similar to the run function, except that the return value is:

  • The run function returns the value/expression of the last line
  • The apply function returns the object itself passed in

5.2 Application Scenarios

Object instance initialization requires the assignment of properties in the object & return the object

5.3 Example

// run function val people = people (" Carson ", 25) val result = people? .run{ println("my name is $name, I am $age years old") I am $age years old") I am $age years old Run {println("my name is $name, I am $age years old") 999}} val result = people?Copy the code

That concludes Kotlin’s discussion of several useful built-in functions for extending and facilitating developer coding.


conclusion

  • And I’m going to continue with thatAndroidThe development of theKotlinThe relevant knowledge, interested students can continue to pay attention to my blogDevelopment notes for Carson_Ho

Please give the top/comment a thumbs up! Because your encouragement is the biggest power that I write!