From 1ac9815d764733076a5b58c79f27bf232f9bd908 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 12 Jan 2017 04:57:38 +0300 Subject: [PATCH] Introduce new scope function: 'also'. Works like 'apply' but with 'it' instead of 'this' inside lambda. #KT-6903 --- libraries/stdlib/src/kotlin/util/Standard.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/util/Standard.kt b/libraries/stdlib/src/kotlin/util/Standard.kt index 64a04a5085b..f6a12f5d7dc 100644 --- a/libraries/stdlib/src/kotlin/util/Standard.kt +++ b/libraries/stdlib/src/kotlin/util/Standard.kt @@ -48,13 +48,19 @@ public inline fun with(receiver: T, block: T.() -> R): R = receiver.block @kotlin.internal.InlineOnly public inline fun T.apply(block: T.() -> Unit): T { block(); return this } +/** + * Calls the specified function [block] with `this` value as its argument and returns `this` value. + */ +@kotlin.internal.InlineOnly +@SinceKotlin("1.1") +public inline fun T.also(block: (T) -> Unit): T { block(this); return this } + /** * Calls the specified function [block] with `this` value as its argument and returns its result. */ @kotlin.internal.InlineOnly public inline fun T.let(block: (T) -> R): R = block(this) - /** * Executes the given function [action] specified number of [times]. *