Deprecate times method taking integer and function. Provide top-level repeat method instead.

#KT-7074 Fixed.
This commit is contained in:
Ilya Gorbunov
2015-05-25 20:34:35 +03:00
parent 45c82f2844
commit 310666a617
2 changed files with 15 additions and 2 deletions
@@ -41,14 +41,14 @@ public inline fun <T> 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()
}
}
@@ -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)
}
}