diff --git a/compiler/testData/builtin-classes.txt b/compiler/testData/builtin-classes.txt index f67c7d9da2d..b214069377f 100644 --- a/compiler/testData/builtin-classes.txt +++ b/compiler/testData/builtin-classes.txt @@ -1,6 +1,14 @@ package-fragment kotlin public inline fun Array(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.Array +public inline fun BooleanArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.BooleanArray +public inline fun ByteArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.ByteArray +public inline fun CharArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.CharArray +public inline fun DoubleArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.DoubleArray +public inline fun FloatArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.FloatArray +public inline fun IntArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.IntArray +public inline fun LongArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.LongArray +public inline fun ShortArray(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1): kotlin.ShortArray public inline fun arrayOf(/*0*/ vararg elements: T /*kotlin.Array*/): kotlin.Array public fun arrayOfNulls(/*0*/ size: kotlin.Int): kotlin.Array public fun booleanArrayOf(/*0*/ vararg elements: kotlin.Boolean /*kotlin.BooleanArray*/): kotlin.BooleanArray diff --git a/compiler/testData/codegen/boxWithStdlib/callableReference/function/newArray.kt b/compiler/testData/codegen/boxWithStdlib/callableReference/function/newArray.kt index 8a2b5a41a58..b27d4e68ac9 100644 --- a/compiler/testData/codegen/boxWithStdlib/callableReference/function/newArray.kt +++ b/compiler/testData/codegen/boxWithStdlib/callableReference/function/newArray.kt @@ -1,11 +1,13 @@ +private fun upcast(value: T): T = value + fun box(): String { - (::ByteArray)(10) - (::IntArray)(10) - (::ShortArray)(10) - (::LongArray)(10) - (::DoubleArray)(10) - (::FloatArray)(10) - (::BooleanArray)(10) + upcast<(Int)->ByteArray>(::ByteArray)(10) + upcast<(Int)->IntArray>(::IntArray)(10) + upcast<(Int)->ShortArray>(::ShortArray)(10) + upcast<(Int)->LongArray>(::LongArray)(10) + upcast<(Int)->DoubleArray>(::DoubleArray)(10) + upcast<(Int)->FloatArray>(::FloatArray)(10) + upcast<(Int)->BooleanArray>(::BooleanArray)(10) return "OK" } diff --git a/core/builtins/src/kotlin/ArrayIntrinsics.kt b/core/builtins/src/kotlin/ArrayIntrinsics.kt index 14c7fabe838..ec75c2b5bf8 100644 --- a/core/builtins/src/kotlin/ArrayIntrinsics.kt +++ b/core/builtins/src/kotlin/ArrayIntrinsics.kt @@ -22,14 +22,107 @@ package kotlin */ public inline fun Array(size: Int, init: (Int) -> T): Array { val result = arrayOfNulls(size) - - for (i in 0..size - 1) { + for (i in 0..size - 1) result[i] = init(i) - } - return result as Array } +/** + * Returns an array of [Double] numbers 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 DoubleArray(size: Int, init: (Int) -> Double): DoubleArray { + val result = DoubleArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Float] numbers 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 FloatArray(size: Int, init: (Int) -> Float): FloatArray { + val result = FloatArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Long] numbers 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 LongArray(size: Int, init: (Int) -> Long): LongArray { + val result = LongArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Int] numbers 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 IntArray(size: Int, init: (Int) -> Int): IntArray { + val result = IntArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Char] numbers 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 CharArray(size: Int, init: (Int) -> Char): CharArray { + val result = CharArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Short] numbers 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 ShortArray(size: Int, init: (Int) -> Short): ShortArray { + val result = ShortArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Byte] numbers 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 ByteArray(size: Int, init: (Int) -> Byte): ByteArray { + val result = ByteArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Boolean] numbers 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 BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray { + val result = BooleanArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + /** * Returns an empty array of the specified type [T]. */ diff --git a/idea/testData/decompiler/builtIns/kotlin_package.stubs b/idea/testData/decompiler/builtIns/kotlin_package.stubs index fb0a0fa462c..e0795031c9b 100644 --- a/idea/testData/decompiler/builtIns/kotlin_package.stubs +++ b/idea/testData/decompiler/builtIns/kotlin_package.stubs @@ -37,6 +37,238 @@ PsiJetFileStubImpl[package=kotlin] TYPE_REFERENCE: USER_TYPE:[isAbsoluteInRootPackage=false] REFERENCE_EXPRESSION:[referencedName=T] + FUN:[fqName=kotlin.BooleanArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=BooleanArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Boolean] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=BooleanArray] + FUN:[fqName=kotlin.ByteArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=ByteArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Byte] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=ByteArray] + FUN:[fqName=kotlin.CharArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=CharArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Char] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=CharArray] + FUN:[fqName=kotlin.DoubleArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=DoubleArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Double] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=DoubleArray] + FUN:[fqName=kotlin.FloatArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=FloatArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Float] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=FloatArray] + FUN:[fqName=kotlin.IntArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=IntArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=IntArray] + FUN:[fqName=kotlin.LongArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=LongArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Long] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=LongArray] + FUN:[fqName=kotlin.ShortArray, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=false, isExtension=false, isTopLevel=true, name=ShortArray] + MODIFIER_LIST:[public inline] + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=size] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=init] + TYPE_REFERENCE: + FUNCTION_TYPE: + VALUE_PARAMETER_LIST: + VALUE_PARAMETER:[fqName=null, hasDefaultValue=false, hasValOrVar=false, isMutable=false, name=null] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Int] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=Short] + TYPE_REFERENCE: + USER_TYPE:[isAbsoluteInRootPackage=false] + USER_TYPE:[isAbsoluteInRootPackage=false] + REFERENCE_EXPRESSION:[referencedName=kotlin] + REFERENCE_EXPRESSION:[referencedName=ShortArray] FUN:[fqName=kotlin.arrayOf, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=true, isExtension=false, isTopLevel=true, name=arrayOf] MODIFIER_LIST:[public inline] TYPE_PARAMETER_LIST: diff --git a/idea/testData/decompiler/builtIns/kotlin_package.text b/idea/testData/decompiler/builtIns/kotlin_package.text index 419bf44c0b8..13ddef7c0b2 100644 --- a/idea/testData/decompiler/builtIns/kotlin_package.text +++ b/idea/testData/decompiler/builtIns/kotlin_package.text @@ -5,6 +5,22 @@ package kotlin public inline fun Array(size: kotlin.Int, init: (kotlin.Int) -> T): kotlin.Array { /* compiled code */ } +public inline fun BooleanArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Boolean): kotlin.BooleanArray { /* compiled code */ } + +public inline fun ByteArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Byte): kotlin.ByteArray { /* compiled code */ } + +public inline fun CharArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Char): kotlin.CharArray { /* compiled code */ } + +public inline fun DoubleArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Double): kotlin.DoubleArray { /* compiled code */ } + +public inline fun FloatArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Float): kotlin.FloatArray { /* compiled code */ } + +public inline fun IntArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Int): kotlin.IntArray { /* compiled code */ } + +public inline fun LongArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Long): kotlin.LongArray { /* compiled code */ } + +public inline fun ShortArray(size: kotlin.Int, init: (kotlin.Int) -> kotlin.Short): kotlin.ShortArray { /* compiled code */ } + public inline fun arrayOf(vararg elements: T): kotlin.Array { /* compiled code */ } public fun arrayOfNulls(size: kotlin.Int): kotlin.Array { /* compiled code */ } diff --git a/js/js.libraries/src/core/kotlin.kt b/js/js.libraries/src/core/kotlin.kt index 1527b72371f..527ea3148b8 100644 --- a/js/js.libraries/src/core/kotlin.kt +++ b/js/js.libraries/src/core/kotlin.kt @@ -2,21 +2,114 @@ package kotlin import java.util.* +// TODO: @library("arrayFromFun") /** * 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) { + for (i in 0..size - 1) result[i] = init(i) - } - return result as Array } +/** + * Returns an array of [Double] numbers 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 DoubleArray(size: Int, init: (Int) -> Double): DoubleArray { + val result = DoubleArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Float] numbers 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 FloatArray(size: Int, init: (Int) -> Float): FloatArray { + val result = FloatArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Long] numbers 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 LongArray(size: Int, init: (Int) -> Long): LongArray { + val result = LongArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Int] numbers 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 IntArray(size: Int, init: (Int) -> Int): IntArray { + val result = IntArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Char] numbers 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 CharArray(size: Int, init: (Int) -> Char): CharArray { + val result = CharArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Short] numbers 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 ShortArray(size: Int, init: (Int) -> Short): ShortArray { + val result = ShortArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Byte] numbers 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 ByteArray(size: Int, init: (Int) -> Byte): ByteArray { + val result = ByteArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + +/** + * Returns an array of [Boolean] numbers 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 BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray { + val result = BooleanArray(size) + for (i in 0..size - 1) + result[i] = init(i) + return result +} + /** * Returns an empty array of the specified type [T]. */ diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 57fda21c08f..4df225ab7da 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -46,6 +46,14 @@ class ArraysTest { assertEquals(expected, arr[1]) } + @test fun byteArrayInit() { + val arr = ByteArray(2) { it.toByte() } + + assertEquals(2, arr.size) + assertEquals(0.toByte(), arr[0]) + assertEquals(1.toByte(), arr[1]) + } + @test fun shortArray() { val arr = ShortArray(2) @@ -55,6 +63,14 @@ class ArraysTest { assertEquals(expected, arr[1]) } + @test fun shortArrayInit() { + val arr = ShortArray(2) { it.toShort() } + + assertEquals(2, arr.size) + assertEquals(0.toShort(), arr[0]) + assertEquals(1.toShort(), arr[1]) + } + @test fun intArray() { val arr = IntArray(2) @@ -62,7 +78,15 @@ class ArraysTest { assertEquals(0, arr[0]) assertEquals(0, arr[1]) } + + @test fun intArrayInit() { + val arr = IntArray(2) { it.toInt() } + assertEquals(2, arr.size) + assertEquals(0.toInt(), arr[0]) + assertEquals(1.toInt(), arr[1]) + } + @test fun longArray() { val arr = LongArray(2) @@ -72,6 +96,14 @@ class ArraysTest { assertEquals(expected, arr[1]) } + @test fun longArrayInit() { + val arr = LongArray(2) { it.toLong() } + + assertEquals(2, arr.size) + assertEquals(0.toLong(), arr[0]) + assertEquals(1.toLong(), arr[1]) + } + @test fun floatArray() { val arr = FloatArray(2) @@ -80,6 +112,14 @@ class ArraysTest { assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } + + @test fun floatArrayInit() { + val arr = FloatArray(2) { it.toFloat() } + + assertEquals(2, arr.size) + assertEquals(0.toFloat(), arr[0]) + assertEquals(1.toFloat(), arr[1]) + } @test fun doubleArray() { val arr = DoubleArray(2) @@ -89,6 +129,14 @@ class ArraysTest { assertEquals(0.0, arr[1]) } + @test fun doubleArrayInit() { + val arr = DoubleArray(2) { it.toDouble() } + + assertEquals(2, arr.size) + assertEquals(0.toDouble(), arr[0]) + assertEquals(1.toDouble(), arr[1]) + } + @test fun charArray() { val arr = CharArray(2) @@ -98,6 +146,14 @@ class ArraysTest { assertEquals(expected, arr[1]) } + @test fun charArrayInit() { + val arr = CharArray(2) { 'a' + it } + + assertEquals(2, arr.size) + assertEquals('a', arr[0]) + assertEquals('b', arr[1]) + } + @test fun booleanArray() { val arr = BooleanArray(2) assertEquals(arr.size, 2) @@ -105,6 +161,14 @@ class ArraysTest { assertEquals(false, arr[1]) } + @test fun booleanArrayInit() { + val arr = BooleanArray(2) { it % 2 == 0 } + + assertEquals(2, arr.size) + assertEquals(true, arr[0]) + assertEquals(false, arr[1]) + } + @test fun min() { expect(null, { arrayOf().min() }) expect(1, { arrayOf(1).min() })