Calls the specified function [block] with `this` value as its receiver and returns `this` value. For detailed usage information see the documentation for [scope functions] (https://kotlinlang.org/docs/reference/scope-functions.html#apply). @kotlin.internal.InlineOnly public inline fun <T> T....
InvocationKind.EXACTLY_ONCE)4}5block()6returnthis7}89/**10* Calls the specified function [block] with `this` value as its argument and returns `this` value.11*12* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-...
/** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */@kotlin.internal.InlineOnlypublic inline fun T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this} 1. 举个例子...
// 使用Javaif(mVar!=null){mVar.function1();mVar.function2();mVar.function3();}// 使用kotlin(无使用let函数)mVar?.function1()mVar?.function2()mVar?.function3()// 使用kotlin(使用let函数)// 方便了统一判空的处理 & 确定了mVar变量的作用域mVar?.let{it.function1()it.function2()it.functio...
其中with的定义比较特殊,它是唯一一个不是扩展函数的Scope Function,但是区别仅仅是将receiver当参数传入。如下代码,run的receiver写在外边,而with的receiver写在里面,除此之外完全相同。 val size4: Int= str.run { println(this) this.length } val size5: Int= with(str){ ...
* Calls the specified function [block] and returns its result. * * For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#run). */@kotlin.internal.InlineOnlypublicinlinefun<R>run(block:()->R):R{contract{callsInPl...
( 参数和返回值为函数的函数 )Calls the specified function [block] with `this` value as its receiver and returns `this` value.For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply).@kotlin.internal.InlineOnly...
前言 本文仅简单描述一下kotlin中常用到的scope function,如apply,let,run,with,also等函数的常用方法和选取。即使很多情况下选择不同函数,也同样都能达到最终效果,具体选择哪个函数我们不会严格约束,但如果你是对代码规范要求比较高的,最好建立良好的代码习惯。 一般
with函数也是一个单独的函数,并不是Kotlin中的extension,指定的T作为闭包的receiver,使用参数中闭包的返回结果 /** * Calls the specified function [block] with the given [receiver] as its receiver and returns its result. */ @kotlin.internal.InlineOnly ...
public final class ApplyFunctionKt { public static final void main(@NotNull String[] args) { Intrinsics.checkParameterIsNotNull(args, "args"); User user = new User("Kotlin", 1, "1111111"); String var5 = "my name is " + user.getName() + ", I am " + user.getAge() + " year...