Make Array.size() a function instead of a property

Also add a deprecated extension property to help migration. This is done to
unify getting size of arrays and collections
This commit is contained in:
Alexander Udalov
2014-11-14 18:02:04 +03:00
parent bc238d5f4c
commit 128c938965
67 changed files with 677 additions and 625 deletions
@@ -6,55 +6,55 @@ fun foo(a: Any): Int {
a.set(0, 1)
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is ShortArray) {
a.get(0)
a.set(0, 1)
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is ByteArray) {
a.get(0)
a.set(0, 1)
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is FloatArray) {
a.get(0)
a.set(0, 1.toFloat())
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is DoubleArray) {
a.get(0)
a.set(0, 1.0)
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is BooleanArray) {
a.get(0)
a.set(0, false)
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is CharArray) {
a.get(0)
a.set(0, 'a')
a.iterator()
a.indices
return a.size
return a.size()
}
if (a is Array<*>) {
if (a.size > 0) a.get(0)
if (a.size() > 0) a.get(0)
a.iterator()
a.indices
return a.size
return a.size()
}
return 0
@@ -22,11 +22,8 @@ fun box() : String {
catch (e : ArrayIndexOutOfBoundsException) {
// No more tests to process
}
System.out?.println(n)
return if(n == 2) "OK" else "fail"
}
fun thirdElementIsThree(a : IntArray) =
// Problematic code does not compile
// a.size >= 3 & a[2] == 3
a.size >= 3 && a[2] == 3
a.size() >= 3 && a[2] == 3
@@ -1,3 +1,3 @@
val <T> Array<T>.length : Int get() = this.size
val <T> Array<T>.length : Int get() = this.size()
fun box() = if(Array(10, {1}).length == 10) "OK" else "fail"
@@ -1,7 +1,7 @@
public class StockMarketTableModel() {
public fun getColumnCount() : Int {
return COLUMN_TITLES?.size!!
return COLUMN_TITLES?.size()!!
}
class object {
@@ -4,7 +4,7 @@ fun IntArray.swap(i:Int, j:Int) {
this[j] = temp
}
fun IntArray.quicksort() = quicksort(0, size-1)
fun IntArray.quicksort() = quicksort(0, size()-1)
fun IntArray.quicksort(L: Int, R:Int) {
val m = this[(L + R) / 2]
@@ -34,7 +34,7 @@ fun box() : String {
a[2*i+1] = -2*i-1
}
a.quicksort()
for(i in 0..a.size-2) {
for(i in 0..a.size()-2) {
if (a[i] > a[i+1]) return "Fail $i: ${a[i]} > ${a[i+1]}"
}
return "OK"
@@ -13,7 +13,7 @@ fun box(): String {
if (Game.foo() != Game.ROCK) return "Fail 1"
// TODO: fix initialization order and uncomment (KT-5761)
// if (Game.bar != Game.PAPER) return "Fail 2: ${Game.bar}"
if (Game.values().size != 3) return "Fail 3"
if (Game.values().size() != 3) return "Fail 3"
if (Game.valueOf("SCISSORS") != Game.SCISSORS) return "Fail 4"
return "OK"
}
@@ -1,7 +1,7 @@
enum class Empty
fun box(): String {
if (Empty.values().size != 0) return "Fail: ${Empty.values()}"
if (Empty.values().size() != 0) return "Fail: ${Empty.values()}"
try {
val found = Empty.valueOf("nonExistentEntry")
@@ -1,9 +1,9 @@
class A {
fun get(vararg x: Int) = x.size
fun get(vararg x: Int) = x.size()
}
class B {
fun get(vararg x: Unit) = x.size
fun get(vararg x: Unit) = x.size()
}
fun test1(a: A): Int {
@@ -7,7 +7,7 @@ fun checkLess(x: Array<Int>, y: Array<Int>) = when {
else -> "OK"
}
fun Array<Int>.compareTo(other: Array<Int>) = size - other.size
fun Array<Int>.compareTo(other: Array<Int>) = size() - other.size()
fun box(): String {
val a = Array<Int>(0, {0})
@@ -27,4 +27,4 @@ fun bottlesOfBeer(count : Int) : String {
fun print(message : String) { System.out?.print(message) }
fun println(message : String) { System.out?.println(message) }
fun StringBuilder.plusAssign(o : Any) { append(o) }
val <T> Array<T>.isEmpty : Boolean get() = size == 0
val <T> Array<T>.isEmpty : Boolean get() = size() == 0
@@ -1,8 +1,8 @@
fun box() : String {
val data = Array<Array<Boolean>>(3) { Array<Boolean>(4, {false}) }
for(d in data) {
if(d.size != 4) return "fail"
System.out?.println(d)
if(d.size() != 4) return "fail"
for(b in d) if (b) return "fail"
}
return "OK"
@@ -19,7 +19,7 @@ fun box() : String {
P(""""" """"", "\"\" \"\"")
)
for (i in 0..data.size-1) {
for (i in 0..data.size()-1) {
val p = data[i]
if (p.actual != p.expected) return "Fail at #$i. actual='${p.actual}', expected='${p.expected}'"
}
@@ -39,7 +39,7 @@ class MutableArray<T>(length: Int, init : (Int) -> T) : ReadOnlyArray<T>, WriteO
override fun set(index : Int, value : T) : Unit { array[index] = value }
override val size : Int
get() = array.size
get() = array.size()
}
fun box() : String {
@@ -47,10 +47,10 @@ fun box() : String {
a [0] = 10
a.set(1, 2, 13)
a [3] = 40
System.out?.println(a.iterator())
System.out?.println(a.iterator().hasNext())
a.iterator()
a.iterator().hasNext()
for(el in a) {
System.out?.println(el)
val fl = el
}
return "OK"
}
@@ -4,12 +4,9 @@ import java.util.HashSet
fun iarray(vararg a : Int) = a // BUG
fun IntArray.lastIndex() = size - 1
fun IntArray.lastIndex() = size() - 1
fun box() : String {
// Problematic code does not compile
// val vals = iarray(789, 678, 567, 456, 345, 234, 123, 012)
val vals = iarray(789, 678, 567, 456, 345, 234, 123, 12)
val diffs = HashSet<Int>()
for (i in vals.indices)
@@ -3,13 +3,13 @@ fun foo(a: Int, vararg b: Int, f: (IntArray) -> String): String {
}
fun box(): String {
val test1 = foo(1) {a -> "" + a.size}
val test1 = foo(1) {a -> "" + a.size()}
if (test1 != "test1 0") return test1
val test2 = foo(2, 2) {a -> "" + a.size}
val test2 = foo(2, 2) {a -> "" + a.size()}
if (test2 != "test2 1") return test2
val test3 = foo(3, 2, 3) {a -> "" + a.size}
val test3 = foo(3, 2, 3) {a -> "" + a.size()}
if (test3 != "test3 2") return test3
return "OK"
@@ -22,11 +22,11 @@ fun box(): String {
val e = javaClass<E>()
val e1 = e.getDeclaredField(E.E1.toString()).getAnnotations()
if (e1.size != 1) return "Fail E1 size: ${e1.toList()}"
if (e1.size() != 1) return "Fail E1 size: ${e1.toList()}"
if (e1[0].annotationType() != javaClass<First>()) return "Fail E1: ${e1.toList()}"
val e2 = e.getDeclaredField(E.E2.toString()).getAnnotations()
if (e2.size != 1) return "Fail E2 size: ${e2.toList()}"
if (e2.size() != 1) return "Fail E2 size: ${e2.toList()}"
if (e2[0].annotationType() != javaClass<Second>()) return "Fail E2: ${e2.toList()}"
return (e2[0] as Second).value
@@ -4,12 +4,12 @@ fun box() : String {
for (method in javaClass<A>().getDeclaredMethods()) {
if (method.getName() == "copy"){
val parameterTypes = method.getParameterTypes()
if (parameterTypes != null && parameterTypes.size == 2) {
if (parameterTypes != null && parameterTypes.size() == 2) {
val copy = A(1, "a").copy(a = 2, b = "b")
return "OK"
}
else {
return "Method copy has " + (if (parameterTypes == null) "0" else parameterTypes.size) + " parameters, expected 2"
return "Method copy has " + (if (parameterTypes == null) "0" else parameterTypes.size()) + " parameters, expected 2"
}
}
}
@@ -9,7 +9,7 @@ fun box(): String {
return "Fail: an exception should be thrown"
} catch (e: IllegalStateException) {
val st = (e as java.lang.Throwable).getStackTrace()
if (st.size < 5) {
if (st.size() < 5) {
return "Fail: very small stack trace, should at least have current function and JUnit reflective calls: ${Arrays.toString(st)}"
}
val top = st[0]
@@ -18,7 +18,7 @@ fun box(): String {
val method = javaClass<A>().getDeclaredMethod("a", javaClass<B>())
val genericParameterTypes = method.getGenericParameterTypes()
if (genericParameterTypes.size != 1) return "Wrong number of generic parameters"
if (genericParameterTypes.size() != 1) return "Wrong number of generic parameters"
if (genericParameterTypes[0].toString() != "T") return "Wrong parameter type ${genericParameterTypes[0].toString()}"
@@ -1,6 +1,6 @@
import kotlin.test.assertEquals
fun <T> foo(vararg a: T) = a.size
fun <T> foo(vararg a: T) = a.size()
inline fun <reified T> bar(a: Array<T>, block: () -> T): Array<T> {
assertEquals(4, foo(*a, block(), block()))
@@ -1,6 +1,6 @@
import kotlin.test.assertEquals
fun <T> foo(vararg a: T) = a.size
fun <T> foo(vararg a: T) = a.size()
inline fun <reified T> bar(block: () -> T): Array<T> {
assertEquals(2, foo(block(), block()))
@@ -20,7 +20,7 @@ fun box(): String {
assertEquals("234", b.map { it.toString() }.join(""))
val c: Array<String> = empty()
assertEquals(0, c.size)
assertEquals(0, c.size())
return "OK"
}