Make Array<T>(size, init) a constructor of Array

It's not marked as inline, this is why 'crossinline' was added in
jaggedArray.kt/jaggedDeep.kt. Will be fixed in the following commits
This commit is contained in:
Alexander Udalov
2016-01-21 22:22:00 +03:00
parent 18238bd6f3
commit 04026dbe84
15 changed files with 123 additions and 380 deletions
+3 -1
View File
@@ -22,8 +22,10 @@ package kotlin
* standard library functions.
* See [Kotlin language documentation](http://kotlinlang.org/docs/reference/basic-types.html#arrays)
* for more information on arrays.
* @constructor Creates a new array with the specified [size], where each element is calculated by calling the specified
* [init] function. The [init] function returns an array element given its index.
*/
public class Array<T> private (): Cloneable {
public class Array<T>(size: Int, init: (Int) -> T): Cloneable {
/**
* Returns the array element at the specified [index]. This method can be called using the
* index operator:
+9 -23
View File
@@ -18,66 +18,52 @@ package kotlin
import kotlin.internal.PureReifiable
/**
* Returns an array with the specified [size], where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun <reified @PureReifiable T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
for (i in 0..size - 1)
result[i] = init(i)
return result as Array<T>
}
/**
* Returns an empty array of the specified type [T].
*/
public inline fun <reified @PureReifiable T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
// Array "constructor"
/**
* Returns an array containing the specified elements.
*/
public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T) : Array<T> = elements as Array<T>
public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T): Array<T> = elements as Array<T>
// "constructors" for primitive types array
/**
* Returns an array containing the specified [Double] numbers.
*/
public fun doubleArrayOf(vararg elements: Double) : DoubleArray = elements
public fun doubleArrayOf(vararg elements: Double): DoubleArray = elements
/**
* Returns an array containing the specified [Float] numbers.
*/
public fun floatArrayOf(vararg elements: Float) : FloatArray = elements
public fun floatArrayOf(vararg elements: Float): FloatArray = elements
/**
* Returns an array containing the specified [Long] numbers.
*/
public fun longArrayOf(vararg elements: Long) : LongArray = elements
public fun longArrayOf(vararg elements: Long): LongArray = elements
/**
* Returns an array containing the specified [Int] numbers.
*/
public fun intArrayOf(vararg elements: Int) : IntArray = elements
public fun intArrayOf(vararg elements: Int): IntArray = elements
/**
* Returns an array containing the specified characters.
*/
public fun charArrayOf(vararg elements: Char) : CharArray = elements
public fun charArrayOf(vararg elements: Char): CharArray = elements
/**
* Returns an array containing the specified [Short] numbers.
*/
public fun shortArrayOf(vararg elements: Short) : ShortArray = elements
public fun shortArrayOf(vararg elements: Short): ShortArray = elements
/**
* Returns an array containing the specified [Byte] numbers.
*/
public fun byteArrayOf(vararg elements: Byte) : ByteArray = elements
public fun byteArrayOf(vararg elements: Byte): ByteArray = elements
/**
* Returns an array containing the specified boolean values.
*/
public fun booleanArrayOf(vararg elements: Boolean) : BooleanArray = elements
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray = elements
@@ -18,6 +18,14 @@ package kotlin.jvm.internal
@Suppress("unused")
object IntrinsicArrayConstructors {
private inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result as Array<T>
}
private inline fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = DoubleArray(size)
for (i in 0..size - 1) {