Turn from: 54615036
Kotlin's Let,apply,with,run function difference rearrangement
Rearrange the differences between the various functions, please go here.
void below
For a long time have been using the Kotlin language, but also purely using simple syntax, and recently sometimes written code, the editor automatically prompts to use let and other functions, and then dedicated to spend a little time to study the next.
Let
First let () is defined as the default current object as the IT parameter of the closure, the return value is the last line inside the function, or the specified return
fun <T, R> T.let(f: (T) -> R): R = f(this)
Simple example:
fun testLet ( ): Int {//fun <t, r> t.let< Span class= "Hljs-params" > (f: (T) R): r {f (this"} "testlet". Let {println (IT) println (IT) println (IT) return 1}}//Run result//testlet//testlet// Testlet
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
You can look at the last generated class file, the code is formatted, and the compiler just adds the contents of let behind our original variable.
public static final int testLet() { String str1 = "testLet"; String it = (String)str1; int $i$a$1$let; System.out.println(it); System.out.println(it); System.out.println(it); return 1;}
Come up with some complicated examples.
fun testLet ( ): Int {//fun <t, r> t.let< Span class= "Hljs-params" > (f: (T) R): r {f (this"} "testlet". Let {if (Random (). Nextboolean ()) { Println (IT) return 1} else {println (IT) return 2}}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
The compiled class file
public Span class= "Hljs-keyword" >static final int testlet () {String str1 = "Testlet"; String it = (string) str1; int $i $a$1$let; if (new Random (). Nextboolean ()) {System. OUT.PRINTLN (IT); return 1;} System. out.println (IT); return 2;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Apply
The Apply function calls the Apply function of an object, and within the scope of the function, any method of the object can be called, and the object is returned
fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
code example
Fun testapply () {Fun <T> t.apply(F:t. (), Unit):R =F();ReturnThis}arraylist<String>().apply {add ( "testapply") add ( "testapply") add ( "testapply") println ( "this =" + this)}. let {println (IT)}}//run result//this = [testapply, testapply, testapply]//[testapply, testApply, testapply]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
The compiled class file
PublicStaticfinal void Testapply () {ArrayList LocalArrayList1 =new ArrayList (); ArrayList localArrayList2 = (ArrayList) localArrayList1; int $i $a $1 $apply; ArrayList $receiver; $receiver. Add ( "testapply"); $receiver. Add ( "testapply"); $receiver. Add ( "testapply"); String str = "this =" + $receiver; System.out.println (str); LocalArrayList1 = LocalArrayList1; ArrayList it = (ArrayList) localArrayList1; int $i $a $2 $let; SYSTEM.OUT.PRINTLN (IT); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
With
The WITH function is a separate function, not a extension in Kotlin, so the calling method is a bit different, the return is the last line, and then you can call the object's methods directly, feeling like a combination of let and apply.
with(receiver: T, f: T.() -> R): R = receiver.f()
code example:
Fun Testwith () {Fun <t, r>With(Receiver:t, F:t. () R):R =Receiver.F()with (arraylist<string> ()) { Add ( "Testwith") add (" Testwith ") add ( Span class= "hljs-string" > "Testwith") println (" this = "+ this)}. let {println (IT)}}//run result//this = [testwith, testwith, testwith]//kotlin. Unit
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
class file
PublicStaticfinal void Testwith () {Object Localobject =new ArrayList (); ArrayList LocalArrayList1 = (ArrayList) localobject; int $i $a $1 $with; ArrayList $receiver; $receiver. Add ( "Testwith"); $receiver. Add ( "Testwith"); $receiver. Add ( "Testwith"); String str = "this =" + $receiver; System.out.println (str); Localobject = unit.instance; Unit it = (unit) Localobject; int $i $a $2 $let; SYSTEM.OUT.PRINTLN (IT); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Run
The run function is much like the Apply function, except that the run function uses the return of the last line, and apply returns the current object.
fun <T, R> T.run(f: T.() -> R): R = f()
code example
fun TestRun ( {//fun <t, r> t.run (f:t. () R): r = f () " testrun ". run {println ( "this =" + this)}. let {println (IT)}}//run result//this = testrun//kotlin. Unit
class file
public static finalvoid TestRun () {Object localobject="TestRun";String str1= (String) Localobject; Int $i $a $1 $run; string $receiver; string str2 = "this =" + $receiver; System.out.println (str2); localobject = unit= (unit) localobject; int $i $a $2 $let; System.out.println (it);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Summarize
How, is not looking dizzy, it's okay, let's summarize.
Name of function |
definition |
Parameters |
return value |
extension |
other |
Let |
Fun T.let (f: (T), R): R = f (This) |
It |
Closed Packet return |
Is |
|
Apply |
Fun t.apply (F:t (), Unit): T {f (); return this} |
None, you can use this |
This |
Is |
|
With |
Fun with (Receiver:t, F:t. () R): R = RECEIVER.F () |
None, you can use this |
Closed Packet return |
Whether |
Call mode differs from other |
Run |
Fun T.run (f:t. () R): R = f () |
None, you can use this |
Closed Packet return |
Is |
Let,apply,with,run function Difference of kotlin (RPM)