diff --git a/core/builtins/src/kotlin/ArrayIntrinsics.kt b/core/builtins/src/kotlin/ArrayIntrinsics.kt new file mode 100644 index 00000000000..14c7fabe838 --- /dev/null +++ b/core/builtins/src/kotlin/ArrayIntrinsics.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin + +/** + * 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 Array(size: Int, init: (Int) -> T): Array { + val result = arrayOfNulls(size) + + for (i in 0..size - 1) { + result[i] = init(i) + } + + return result as Array +} + +/** + * Returns an empty array of the specified type [T]. + */ +public inline fun emptyArray(): Array = arrayOfNulls(0) as Array + + +// Array "constructor" +/** + * Returns an array containing the specified elements. + */ +public inline fun arrayOf(vararg elements: T) : Array = elements as Array + +// "constructors" for primitive types array +/** + * Returns an array containing the specified [Double] numbers. + */ +public fun doubleArrayOf(vararg elements: Double) : DoubleArray = elements + +/** + * Returns an array containing the specified [Float] numbers. + */ +public fun floatArrayOf(vararg elements: Float) : FloatArray = elements + +/** + * Returns an array containing the specified [Long] numbers. + */ +public fun longArrayOf(vararg elements: Long) : LongArray = elements + +/** + * Returns an array containing the specified [Int] numbers. + */ +public fun intArrayOf(vararg elements: Int) : IntArray = elements + +/** + * Returns an array containing the specified characters. + */ +public fun charArrayOf(vararg elements: Char) : CharArray = elements + +/** + * Returns an array containing the specified [Short] numbers. + */ +public fun shortArrayOf(vararg elements: Short) : ShortArray = elements + +/** + * Returns an array containing the specified [Byte] numbers. + */ +public fun byteArrayOf(vararg elements: Byte) : ByteArray = elements + +/** + * Returns an array containing the specified boolean values. + */ +public fun booleanArrayOf(vararg elements: Boolean) : BooleanArray = elements diff --git a/js/js.libraries/src/core/kotlin.kt b/js/js.libraries/src/core/kotlin.kt index e154a415c36..1527b72371f 100644 --- a/js/js.libraries/src/core/kotlin.kt +++ b/js/js.libraries/src/core/kotlin.kt @@ -2,6 +2,27 @@ package kotlin import java.util.* +/** + * 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. + */ +// TODO: @library("arrayFromFun") +public inline fun Array(size: Int, init: (Int) -> T): Array { + val result = arrayOfNulls(size) + + for (i in 0..size - 1) { + result[i] = init(i) + } + + return result as Array +} + +/** + * Returns an empty array of the specified type [T]. + */ +public inline fun emptyArray(): Array = arrayOfNulls(0) as Array + + @library public fun arrayOf(vararg elements: T): Array = noImpl diff --git a/libraries/stdlib/src/kotlin/collections/Arrays.kt b/libraries/stdlib/src/kotlin/collections/Arrays.kt index bab3b2e3adc..2ad5373b6cc 100644 --- a/libraries/stdlib/src/kotlin/collections/Arrays.kt +++ b/libraries/stdlib/src/kotlin/collections/Arrays.kt @@ -10,6 +10,7 @@ import java.util.* * 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. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun Array(size: Int, init: (Int) -> T): Array { val result = arrayOfNulls(size) @@ -23,6 +24,7 @@ public inline fun Array(size: Int, init: (Int) -> T): Array { /** * Returns an empty array of the specified type [T]. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun emptyArray(): Array = arrayOfNulls(0) as Array /** diff --git a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt index c288a9af5d0..695d5ba7eff 100644 --- a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt @@ -10,47 +10,56 @@ import java.nio.charset.Charset /** * Returns an array containing the specified elements. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun arrayOf(vararg elements: T) : Array = elements as Array // "constructors" for primitive types array /** * Returns an array containing the specified [Double] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun doubleArrayOf(vararg elements: Double) : DoubleArray = elements /** * Returns an array containing the specified [Float] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun floatArrayOf(vararg elements: Float) : FloatArray = elements /** * Returns an array containing the specified [Long] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun longArrayOf(vararg elements: Long) : LongArray = elements /** * Returns an array containing the specified [Int] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun intArrayOf(vararg elements: Int) : IntArray = elements /** * Returns an array containing the specified characters. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun charArrayOf(vararg elements: Char) : CharArray = elements /** * Returns an array containing the specified [Short] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun shortArrayOf(vararg elements: Short) : ShortArray = elements /** * Returns an array containing the specified [Byte] numbers. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun byteArrayOf(vararg elements: Byte) : ByteArray = elements /** * Returns an array containing the specified boolean values. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun booleanArrayOf(vararg elements: Boolean) : BooleanArray = elements /**