From 310666a6174c20f3909bf655623ae83c9fecb07f Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 25 May 2015 20:34:35 +0300 Subject: [PATCH] Deprecate times method taking integer and function. Provide top-level repeat method instead. #KT-7074 Fixed. --- libraries/stdlib/src/kotlin/concurrent/Locks.kt | 4 ++-- libraries/stdlib/src/kotlin/util/Integers.kt | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/src/kotlin/concurrent/Locks.kt b/libraries/stdlib/src/kotlin/concurrent/Locks.kt index 0b09b7fd7df..de165615612 100644 --- a/libraries/stdlib/src/kotlin/concurrent/Locks.kt +++ b/libraries/stdlib/src/kotlin/concurrent/Locks.kt @@ -41,14 +41,14 @@ public inline fun ReentrantReadWriteLock.write(action: () -> T): T { val rl = readLock() val readCount = if (getWriteHoldCount() == 0) getReadHoldCount() else 0 - readCount times { rl.unlock() } + repeat(readCount) { rl.unlock() } val wl = writeLock() wl.lock() try { return action() } finally { - readCount times { rl.lock() } + repeat(readCount) { rl.lock() } wl.unlock() } } diff --git a/libraries/stdlib/src/kotlin/util/Integers.kt b/libraries/stdlib/src/kotlin/util/Integers.kt index f56b0b67fcd..4dc3c19941a 100644 --- a/libraries/stdlib/src/kotlin/util/Integers.kt +++ b/libraries/stdlib/src/kotlin/util/Integers.kt @@ -3,6 +3,7 @@ package kotlin /** * Executes the given function [body] the number of times equal to the value of this integer. */ +deprecated("Use repeat(n) { body } instead.") public inline fun Int.times(body : () -> Unit) { var count = this; while (count > 0) { @@ -10,3 +11,15 @@ public inline fun Int.times(body : () -> Unit) { count-- } } + + +/** + * Executes the given function [body] specified number of [times]. + * + * A zero-based index of current iteration is passed as a parameter to [body]. + */ +public inline fun repeat(times: Int, body: (Int) -> Unit) { + for (index in 0..times - 1) { + body(index) + } +} \ No newline at end of file