From effe54d470d7cfe96d088b8e16be90fe613f16c9 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 27 Jul 2017 16:35:59 +0700 Subject: [PATCH] stdlib: Add also function --- backend.native/tests/build.gradle | 4 ++++ .../tests/runtime/basic/standard.kt | 23 +++++++++++++++++++ .../src/main/kotlin/kotlin/util/Standard.kt | 7 ++++++ 3 files changed, 34 insertions(+) create mode 100644 backend.native/tests/runtime/basic/standard.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 7f70260f657..c034d2b15a6 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -1516,6 +1516,10 @@ task main_exception(type: RunKonanTest) { source = "runtime/basic/main_exception.kt" } +task runtime_basic_standard(type: RunKonanTest) { + source = "runtime/basic/standard.kt" +} + task runtime_basic_assert_failed(type: RunKonanTest) { expectedExitStatus = 1 source = "runtime/basic/assert_failed.kt" diff --git a/backend.native/tests/runtime/basic/standard.kt b/backend.native/tests/runtime/basic/standard.kt new file mode 100644 index 00000000000..698517766f5 --- /dev/null +++ b/backend.native/tests/runtime/basic/standard.kt @@ -0,0 +1,23 @@ +class Foo(val bar: Int) + +fun assertEquals(actual: T, expected: T) { + if (actual != expected) throw AssertionError("Assertion failed. Expected value: $expected, actual value: $actual") +} + +fun main(args: Array) { + try { + TODO() + throw AssertionError("TODO() doesn't throw an exception") + } catch(e: NotImplementedError) {} + + val foo = Foo(42) + assertEquals(run { 42 }, 42) + assertEquals(foo.run { bar }, 42) + assertEquals(with(foo) { bar }, 42) + assertEquals(foo.apply { bar }, foo) + assertEquals(foo.also { it.bar }, foo) + assertEquals(foo.let { it.bar }, 42) + var i = 0 + repeat(10) { i++ } + assertEquals(i, 10) +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/util/Standard.kt b/runtime/src/main/kotlin/kotlin/util/Standard.kt index c3d39f92bd1..c7def28d7f3 100644 --- a/runtime/src/main/kotlin/kotlin/util/Standard.kt +++ b/runtime/src/main/kotlin/kotlin/util/Standard.kt @@ -61,6 +61,13 @@ 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. */