Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions libraries/stdlib/jvm/builtins/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,64 @@ public actual fun <reified T> arrayOfNulls(size: Int): Array<T?>

/**
* Returns an array containing the specified elements.
*
* @sample samples.collections.Arrays.Constructors.arrayOfSample
*/
public actual inline fun <reified T> arrayOf(vararg elements: T): Array<T>

/**
* Returns an array containing the specified [Double] numbers.
*
* @sample samples.collections.Arrays.Constructors.doubleArrayOfSample
*/
public actual fun doubleArrayOf(vararg elements: Double): DoubleArray

/**
* Returns an array containing the specified [Float] numbers.
*
* @sample samples.collections.Arrays.Constructors.floatArrayOfSample
*/
public actual fun floatArrayOf(vararg elements: Float): FloatArray

/**
* Returns an array containing the specified [Long] numbers.
*
* @sample samples.collections.Arrays.Constructors.longArrayOfSample
*/
public actual fun longArrayOf(vararg elements: Long): LongArray

/**
* Returns an array containing the specified [Int] numbers.
*
* @sample samples.collections.Arrays.Constructors.intArrayOfSample
*/
public actual fun intArrayOf(vararg elements: Int): IntArray

/**
* Returns an array containing the specified characters.
*
* @sample samples.collections.Arrays.Constructors.charArrayOfSample
*/
public actual fun charArrayOf(vararg elements: Char): CharArray

/**
* Returns an array containing the specified [Short] numbers.
*
* @sample samples.collections.Arrays.Constructors.shortArrayOfSample
*/
public actual fun shortArrayOf(vararg elements: Short): ShortArray

/**
* Returns an array containing the specified [Byte] numbers.
*
* @sample samples.collections.Arrays.Constructors.byteArrayOfSample
*/
public actual fun byteArrayOf(vararg elements: Byte): ByteArray

/**
* Returns an array containing the specified boolean values.
*
* @sample samples.collections.Arrays.Constructors.booleanArrayOfSample
*/
public actual fun booleanArrayOf(vararg elements: Boolean): BooleanArray

Expand Down
104 changes: 104 additions & 0 deletions libraries/stdlib/samples/test/samples/collections/arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,108 @@ class Arrays {

}

class Constructors {
@Sample
fun arrayOfSample() {
val emptyArray = arrayOf<Any>()
assertPrints(emptyArray.contentToString(), "[]")

val doubleArray = arrayOf(1.0, 2.5, 3.14)
assertPrints(doubleArray.contentToString(), "[1.0, 2.5, 3.14]")

val floatArray = arrayOf(1.0f, 2.5f, 3.14f)
assertPrints(floatArray.contentToString(), "[1.0, 2.5, 3.14]")

val longArray = arrayOf(1L, 2L, 3L)
assertPrints(longArray.contentToString(), "[1, 2, 3]")

val intArray = arrayOf(1, 2, 3)
assertPrints(intArray.contentToString(), "[1, 2, 3]")

val charArray = arrayOf('a', 'b', 'c')
assertPrints(charArray.contentToString(), "[a, b, c]")

val shortArray = arrayOf(1, 2, 3)
assertPrints(shortArray.contentToString(), "[1, 2, 3]")

val byteArray = arrayOf(1, 2, 3)
assertPrints(byteArray.contentToString(), "[1, 2, 3]")

val booleanArray = arrayOf(true, false, true)
assertPrints(booleanArray.contentToString(), "[true, false, true]")
}

@Sample
fun doubleArrayOfSample() {
val emptyDoubleArray = doubleArrayOf()
assertPrints(emptyDoubleArray.contentToString(), "[]")

val doubleArray = doubleArrayOf(1.0, 2.5, 3.14)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to showcase that these functions produce an empty array when invoked without any arguments. WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Samples for array functions have been added as requested(Ref a74af86). I renamed the sample methods with a 'Sample' suffix to prevent recursive self-calls, but let me know what you think

assertPrints(doubleArray.contentToString(), "[1.0, 2.5, 3.14]")
}

@Sample
fun floatArrayOfSample() {
val emptyFloatArray = floatArrayOf()
assertPrints(emptyFloatArray.contentToString(), "[]")

val floatArray = floatArrayOf(1.0f, 2.5f, 3.14f)
assertPrints(floatArray.contentToString(), "[1.0, 2.5, 3.14]")
}

@Sample
fun longArrayOfSample() {
val emptyLongArray = longArrayOf()
assertPrints(emptyLongArray.contentToString(), "[]")

val longArray = longArrayOf(1L, 2L, 3L)
assertPrints(longArray.contentToString(), "[1, 2, 3]")
}

@Sample
fun intArrayOfSample() {
val emptyIntArray = intArrayOf()
assertPrints(emptyIntArray.contentToString(), "[]")

val intArray = intArrayOf(1, 2, 3)
assertPrints(intArray.contentToString(), "[1, 2, 3]")
}

@Sample
fun charArrayOfSample() {
val emptyCharArray = charArrayOf()
assertPrints(emptyCharArray.contentToString(), "[]")

val charArray = charArrayOf('a', 'b', 'c')
assertPrints(charArray.contentToString(), "[a, b, c]")
}

@Sample
fun shortArrayOfSample() {
val emptyShortArray = shortArrayOf()
assertPrints(emptyShortArray.contentToString(), "[]")

val shortArray = shortArrayOf(1, 2, 3)
assertPrints(shortArray.contentToString(), "[1, 2, 3]")
}

@Sample
fun byteArrayOfSample() {
val emptyByteArray = byteArrayOf()
assertPrints(emptyByteArray.contentToString(), "[]")

val byteArray = byteArrayOf(1, 2, 3)
assertPrints(byteArray.contentToString(), "[1, 2, 3]")
}

@Sample
fun booleanArrayOfSample() {
val emptyBooleanArray = booleanArrayOf()
assertPrints(emptyBooleanArray.contentToString(), "[]")

val booleanArray = booleanArrayOf(true, false, true)
assertPrints(booleanArray.contentToString(), "[true, false, true]")
}
}

}
18 changes: 18 additions & 0 deletions libraries/stdlib/src/kotlin/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,64 @@ public expect fun <reified T> arrayOfNulls(size: Int): Array<T?>

/**
* Returns an array containing the specified elements.
*
* @sample samples.collections.Arrays.Constructors.arrayOfSample
*/
public expect inline fun <reified T> arrayOf(vararg elements: T): Array<T>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also makes sense to add a similar sample for arrayOf ^

Copy link
Author

@oscarArismendi oscarArismendi Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done on b49f96e

/**
* Returns an array containing the specified [Double] numbers.
*
* @sample samples.collections.Arrays.Constructors.doubleArrayOfSample
*/
public expect fun doubleArrayOf(vararg elements: Double): DoubleArray

/**
* Returns an array containing the specified [Float] numbers.
*
* @sample samples.collections.Arrays.Constructors.floatArrayOfSample
*/
public expect fun floatArrayOf(vararg elements: Float): FloatArray

/**
* Returns an array containing the specified [Long] numbers.
*
* @sample samples.collections.Arrays.Constructors.longArrayOfSample
*/
public expect fun longArrayOf(vararg elements: Long): LongArray

/**
* Returns an array containing the specified [Int] numbers.
*
* @sample samples.collections.Arrays.Constructors.intArrayOfSample
*/
public expect fun intArrayOf(vararg elements: Int): IntArray

/**
* Returns an array containing the specified characters.
*
* @sample samples.collections.Arrays.Constructors.charArrayOfSample
*/
public expect fun charArrayOf(vararg elements: Char): CharArray

/**
* Returns an array containing the specified [Short] numbers.
*
* @sample samples.collections.Arrays.Constructors.shortArrayOfSample
*/
public expect fun shortArrayOf(vararg elements: Short): ShortArray

/**
* Returns an array containing the specified [Byte] numbers.
*
* @sample samples.collections.Arrays.Constructors.byteArrayOfSample
*/
public expect fun byteArrayOf(vararg elements: Byte): ByteArray

/**
* Returns an array containing the specified boolean values.
*
* @sample samples.collections.Arrays.Constructors.booleanArrayOfSample
*/
public expect fun booleanArrayOf(vararg elements: Boolean): BooleanArray

Expand Down