stdlib: Add also function

This commit is contained in:
Ilya Matveev
2017-07-27 16:35:59 +07:00
committed by ilmat192
parent d0ec24a5f2
commit effe54d470
3 changed files with 34 additions and 0 deletions
+4
View File
@@ -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"
@@ -0,0 +1,23 @@
class Foo(val bar: Int)
fun <T> assertEquals(actual: T, expected: T) {
if (actual != expected) throw AssertionError("Assertion failed. Expected value: $expected, actual value: $actual")
}
fun main(args: Array<String>) {
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)
}
@@ -61,6 +61,13 @@ public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block
@kotlin.internal.InlineOnly
public inline fun <T> 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> 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.
*/