stdlib: Introduced Array<T>?.orEmpty() in JVM-stdlib
This commit is contained in:
committed by
Andrey Breslav
parent
73ca75cc0b
commit
da5164acd8
@@ -35,3 +35,6 @@ public fun ByteArray.toString(charset: Charset): String = String(this, charset)
|
|||||||
|
|
||||||
[Intrinsic("kotlin.collections.copyToArray")] public fun <reified T> Collection<T>.copyToArray(): Array<T> =
|
[Intrinsic("kotlin.collections.copyToArray")] public fun <reified T> Collection<T>.copyToArray(): Array<T> =
|
||||||
throw UnsupportedOperationException()
|
throw UnsupportedOperationException()
|
||||||
|
|
||||||
|
/** Returns the List if its not null otherwise returns the empty list */
|
||||||
|
public inline fun <reified T> Array<T>?.orEmpty(): Array<T> = this ?: array<T>()
|
||||||
|
|||||||
@@ -197,4 +197,18 @@ class ArraysJVMTest {
|
|||||||
expect(3000000000000) { longArray(1000000000000, 2000000000000).sum() }
|
expect(3000000000000) { longArray(1000000000000, 2000000000000).sum() }
|
||||||
expect(3.0.toFloat()) { floatArray(1.0.toFloat(), 2.0.toFloat()).sum() }
|
expect(3.0.toFloat()) { floatArray(1.0.toFloat(), 2.0.toFloat()).sum() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test fun orEmptyNull() {
|
||||||
|
val x: Array<String>? = null
|
||||||
|
val xArray: Array<String> = x.orEmpty()
|
||||||
|
expect(0) { xArray.size }
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun orEmptyNotNull() {
|
||||||
|
val x: Array<String>? = array("1", "2")
|
||||||
|
val xArray: Array<String> = x.orEmpty()
|
||||||
|
expect(2) { xArray.size }
|
||||||
|
expect("1") { xArray[0] }
|
||||||
|
expect("2") { xArray[1] }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user