当前位置:网站首页>Let, with, apply, also, run
Let, with, apply, also, run
2022-06-21 17:39:00 【Lighthouse @kuaidao】
Preface
kotlin Several functions commonly used in development ,let,with,run,apply,also , They are all range functions , The specific use scene is blurred , I don't know how to use . Generic expressions are listed here , After decompilation, the bytecode is compared , Convenient memory
| Function name | Definition inline Structure | The object used in the function body | Return value | Is it an extension function | Applicable scenarios |
|---|---|---|---|---|---|
| let | fun <T, R> T.let(block: (T) -> R): R = block(this) | it Refers to the current object | Closure | yes | It is applicable to the treatment of not being null Operation scenario of |
| with | fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block() | this To refer to the current object or omit | Closure | no | Where class objects continuously call class properties and methods |
| run | fun <T, R> T.run(block: T.() -> R): R = block() | this To refer to the current object or omit | Closure | yes | Apply to let,with Function any scenario . |
| apply | fun T.apply(block: T.() -> Unit): T { block(); return this } | this To refer to the current object or omit | this | yes | builder Where mode is used |
| also | fun T.also(block: (T) -> Unit): T { block(this); return this } | it Refers to the current object | this | yes | builder Where mode is used |
Be careful kotlin lambda grammar :
block: (T) -> R lambda Expression declaration .
block: T.() -> R With the receiver lambda Expression declaration , This can be written in block Use in this Keyword represents T Actual parameters Use
fun main() {
val str=123
//① val combinStr=str.let { "$str world" }
//② val combinStr= with(str) {
// "$str world"
// }
//③ val combinStr=str.run { "$str world " }
//④ val combinStr=str.apply { "$str world" }
//⑤ val combinStr=str.also { "$str world" }
println(combinStr)
}
// Generic T/R, [ Enter the reference T return R]
//① fun <T, R> T.let(block: (T) -> R): R = block(this)
//② fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
//③ fun <T, R> T.run(block: T.() -> R): R = block()
//④ fun T.apply(block: T.() -> Unit): T { block(); return this }
//⑤ fun T.also(block: (T) -> Unit): T { block(this); return this }
let
//① fun <T, R> T.let(block: (T) -> R): R = block(this)
fun main() {
val str=123
val combinStr=str.let {
"$str world" }
println(combinStr)
}
package letrunwithalsoapply;
import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {
1, 6, 0},
k = 2,
xi = 2,
d1 = {
"\u0000\u0012\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\u001a\u0006\u0010\u0000\u001a\u00020\u0001\u001a/\u0010\u0002\u001a\u0002H\u0003\"\u0004\b\u0000\u0010\u0004\"\u0004\b\u0001\u0010\u0003*\u0002H\u00042\u0012\u0010\u0005\u001a\u000e\u0012\u0004\u0012\u0002H\u0004\u0012\u0004\u0012\u0002H\u00030\u0006¢\u0006\u0002\u0010\u0007¨\u0006\b"},
d2 = {
"main", "", "let", "R", "T", "block", "Lkotlin/Function1;", "(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;", "untitled2"}
)
public final class LetRunAlsoWithApplyKt {
public static final void main() {
final int str = 123;
String combinStr = (String)let(Integer.valueOf(str), (Function1)(new Function1() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke(Object var1) {
return this.invoke(((Number)var1).intValue());
}
@NotNull
public final String invoke(int it) {
return str + " world";
}
}));
System.out.println(combinStr);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
public static final Object let(Object $this$let, @NotNull Function1 block) {
Intrinsics.checkNotNullParameter(block, "block");
return block.invoke($this$let);
}
}
with
//② fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
fun main() {
val str=123
val combinStr= with(str) {
"$str world"
}
println(combinStr)
}
package letrunwithalsoapply;
import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {
1, 6, 0},
k = 2,
xi = 2,
d1 = {
"\u0000\u0016\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u001a\u0006\u0010\u0000\u001a\u00020\u0001\u001a8\u0010\u0002\u001a\u0002H\u0003\"\u0004\b\u0000\u0010\u0004\"\u0004\b\u0001\u0010\u00032\u0006\u0010\u0005\u001a\u0002H\u00042\u0017\u0010\u0006\u001a\u0013\u0012\u0004\u0012\u0002H\u0004\u0012\u0004\u0012\u0002H\u00030\u0007¢\u0006\u0002\b\b¢\u0006\u0002\u0010\t¨\u0006\n"},
d2 = {
"main", "", "with", "R", "T", "receiver", "block", "Lkotlin/Function1;", "Lkotlin/ExtensionFunctionType;", "(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;", "untitled2"}
)
public final class LetRunAlsoWithApplyKt {
public static final void main() {
final int str = 123;
String combinStr = (String)with(Integer.valueOf(str), (Function1)(new Function1() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke(Object var1) {
return this.invoke(((Number)var1).intValue());
}
@NotNull
public final String invoke(int $this$with) {
return str + " world";
}
}));
System.out.println(combinStr);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
public static final Object with(Object receiver, @NotNull Function1 block) {
Intrinsics.checkNotNullParameter(block, "block");
return block.invoke(receiver);
}
}
run
```kotlin
//③ fun <T, R> T.run(block: T.() -> R): R = block()
fun main() {
val str=123
val combinStr=str.run {
"$str world " }
println(combinStr)
}
package letrunwithalsoapply;
import kotlin.Metadata;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {
1, 6, 0},
k = 2,
xi = 2,
d1 = {
"\u0000\u0016\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u001a\u0006\u0010\u0000\u001a\u00020\u0001\u001a4\u0010\u0002\u001a\u0002H\u0003\"\u0004\b\u0000\u0010\u0004\"\u0004\b\u0001\u0010\u0003*\u0002H\u00042\u0017\u0010\u0005\u001a\u0013\u0012\u0004\u0012\u0002H\u0004\u0012\u0004\u0012\u0002H\u00030\u0006¢\u0006\u0002\b\u0007¢\u0006\u0002\u0010\b¨\u0006\t"},
d2 = {
"main", "", "run", "R", "T", "block", "Lkotlin/Function1;", "Lkotlin/ExtensionFunctionType;", "(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;", "untitled2"}
)
public final class LetRunAlsoWithApplyKt {
public static final void main() {
final int str = 123;
String combinStr = (String)run(Integer.valueOf(str), (Function1)(new Function1() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke(Object var1) {
return this.invoke(((Number)var1).intValue());
}
@NotNull
public final String invoke(int $this$run) {
return str + " world ";
}
}));
System.out.println(combinStr);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
public static final Object run(Object $this$run, @NotNull Function1 block) {
Intrinsics.checkNotNullParameter(block, "block");
return block.invoke($this$run);
}
}
apply
//④ fun T.apply(block: T.() -> Unit): T { block(); return this }
fun main() {
val str=123
val combinStr=str.apply {
"$str world" }
println(combinStr)
}
import kotlin.Metadata;
@Metadata(
mv = {
1, 6, 0},
k = 2,
xi = 2,
d1 = {
"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"},
d2 = {
"main", "", "untitled2"}
)
public final class LetRunAlsoWithApplyKt {
public static final void main() {
int str = 123;
int var4 = false;
(new StringBuilder()).append(str).append(" world").toString();
System.out.println(str);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
}
also
//⑤fun T.also(block: (T) -> Unit): T { block(this); return this }
fun main() {
val str=123
val combinStr=str.also {
"$str world" }
println(combinStr)
}
package letrunwithalsoapply;
import kotlin.Metadata;
@Metadata(
mv = {
1, 6, 0},
k = 2,
xi = 2,
d1 = {
"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"},
d2 = {
"main", "", "untitled2"}
)
public final class LetRunAlsoWithApplyKt {
public static final void main() {
int str = 123;
int var4 = false;
(new StringBuilder()).append(str).append(" world").toString();
System.out.println(str);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
}
summary
Basically apply and with All you do is call the arguments of the extension function type on the receiver provided to it . apply The function is declared as an extension function of the receiver , and with The function takes it as its first argument . in addition , apply Return to the recipient itself , and with Return call lambda After the results of the . If you don't care about the return value , this The two methods can be used interchangeably Of
Kotlin Series of let、with、run、apply、also Use of functions
this still it? The rational use of with The operator
边栏推荐
- 软件测试体系学习及构建(13)-测试基础之测试工程师的基本要求
- Kotlin annotation declaration and use
- 【没搞懂路由策略?盘它!】
- Kotlin DSL build
- 欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
- 关于xlrd库的基础操作(新手向)
- PTA l3-032 questions about depth first search and reverse order pair should not be difficult (30 points)
- LeetCode_字符串_简单_387. 字符串中的第一个唯一字符
- 一些细节
- The node server res.end() writes Chinese, and the solution to the problem of garbled code in the client
猜你喜欢

Bm95 points candy problem

#Vscode工具#

焱融科技 YRCloudFile 与安腾普完成兼容认证,共创存储新蓝图

Kubernetes + Yanrong SaaS data service platform, personalized demand support has never been lost

PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分

BM95 分糖果问题

Redis6.0新特性(上)

硅橡胶玻纤管EN45545防火试验的难易程度

Iso8191 test is mentioned in as 3744.1. Are the two tests the same?

拉格朗日插值
随机推荐
加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证
How to connect the Internet - FTTH
Kubernetes + Yanrong SaaS data service platform, personalized demand support has never been lost
拉格朗日插值
wcdma与LTE的区别
Sorting out Android kotlin generic knowledge points
BFS与DFS
焱融科技 YRCloudFile 与安腾普完成兼容认证,共创存储新蓝图
What is the process of en 1101 flammability test for curtains?
Vscade tool
Android kotlin泛型知识点梳理
Behind Yanrong SaaS service platform, which is as stable as a rock, is the rise of data ecology
BM23 二叉树的前序遍历
The main relations and differences between Poisson sampling and Bernoulli sampling
fs. Readfile() and fs writeFile()
Compose 中的附带效应
一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策
Common formula of derivative__ Common formulas of indefinite integral
BFS and DFS
我的小工具-卡片学习APP 完成啦