Make CharSequence.length a function instead of property
And String.length as well. This is done for JVM interoperability: java.lang.CharSequence is an open class and has a function 'length()' which should be implemented in subclasses somehow. A minor unexpected effect of this is that String.length() is now a compile-time constant (it wasn't such as a property because properties are not supported in compile-time constant evaluation) #KT-3571 Fixed
This commit is contained in:
@@ -4,8 +4,8 @@ fun box() : String {
|
||||
val w = object : Comparator<String?> {
|
||||
|
||||
override fun compare(o1 : String?, o2 : String?) : Int {
|
||||
val l1 : Int = o1?.length ?: 0
|
||||
val l2 = o2?.length ?: 0
|
||||
val l1 : Int = o1?.length() ?: 0
|
||||
val l2 = o2?.length() ?: 0
|
||||
return l1 - l2
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ fun box(): String {
|
||||
if (y != 5) return "Fail 2 $y"
|
||||
|
||||
var z = ""
|
||||
do { z += z.length } while (z.length < 5)
|
||||
do { z += z.length() } while (z.length() < 5)
|
||||
if (z != "01234") return "Fail 3 $z"
|
||||
|
||||
return "OK"
|
||||
|
||||
@@ -12,7 +12,7 @@ public fun testCoalesce() {
|
||||
else -> "Hello world"
|
||||
}
|
||||
|
||||
printlnMock(value.length)
|
||||
printlnMock(value.length())
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
@@ -34,7 +34,7 @@ object StringHandler {
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
a.foo = 42
|
||||
a.foo = a.foo + baz.length
|
||||
a.foo = a.foo + baz.length()
|
||||
a.foo = 239
|
||||
A.bar = baz + a.foo
|
||||
baz + A.bar
|
||||
|
||||
+1
-2
@@ -6,11 +6,10 @@ fun escapeChar(c : Char) : String? = when (c) {
|
||||
}
|
||||
|
||||
tailRecursive fun String.escape(i : Int = 0, result : StringBuilder = StringBuilder()) : String =
|
||||
if (i == length) result.toString()
|
||||
if (i == length()) result.toString()
|
||||
else escape(i + 1, result.append(escapeChar(get(i))))
|
||||
|
||||
fun box() : String {
|
||||
"test me not \\".escape()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -4,5 +4,5 @@ tailRecursive fun String.repeat(num : Int, acc : StringBuilder = StringBuilder()
|
||||
|
||||
fun box() : String {
|
||||
val s = "a".repeat(10000)
|
||||
return if (s.length == 10000) "OK" else "FAIL: ${s.length}"
|
||||
}
|
||||
return if (s.length() == 10000) "OK" else "FAIL: ${s.length()}"
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ fun box(): String {
|
||||
val list = java.util.ArrayList<String>()
|
||||
list.add("0")
|
||||
list[0][0]
|
||||
list[0].length
|
||||
list[0].length()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ fun escapeChar(c : Char) : String? = when (c) {
|
||||
}
|
||||
|
||||
fun String.escape(i : Int = 0, result : String = "") : String =
|
||||
if (i == length) result
|
||||
if (i == length()) result
|
||||
else escape(i + 1, result + escapeChar(get(i)))
|
||||
|
||||
fun box() : String {
|
||||
@@ -19,7 +19,7 @@ fun box() : String {
|
||||
System.out?.println("}");
|
||||
System.out?.println();
|
||||
System.out?.println("fun String.escape(i : Int = 0, result : String = \"\") : String =");
|
||||
System.out?.println(" if (i == length) result");
|
||||
System.out?.println(" if (i == length()) result");
|
||||
System.out?.println(" else escape(i + 1, result + escapeChar(this.get(i)))");
|
||||
System.out?.println();
|
||||
System.out?.println("fun main(args : Array<String>) {");
|
||||
|
||||
@@ -3,7 +3,7 @@ class Test {
|
||||
private val b : String get() = a
|
||||
|
||||
fun outer() : Int {
|
||||
return b.length
|
||||
return b.length()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fun t1() : Boolean {
|
||||
val s1 : String? = "sff"
|
||||
val s2 : String? = null
|
||||
return s1?.length == 3 && s2?.length == null
|
||||
return s1?.length() == 3 && s2?.length() == null
|
||||
}
|
||||
|
||||
fun t2() : Boolean {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class Thing(delegate: CharSequence) : CharSequence by delegate
|
||||
|
||||
fun box(): String {
|
||||
val l = Thing("hello there").length()
|
||||
return if (l == 11) "OK" else "Fail $l"
|
||||
}
|
||||
@@ -14,7 +14,7 @@ fun box() : String {
|
||||
|
||||
val test5 =
|
||||
"""
|
||||
${s.length}
|
||||
${s.length()}
|
||||
"""
|
||||
if (test5 != "\n 3\n") return "Fail 5: $test5"
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ fun box(): String {
|
||||
|
||||
override fun condition(): Boolean {
|
||||
result = "OK"
|
||||
return result.length == 2
|
||||
return result.length() == 2
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fun box(): String {
|
||||
var v = "FAIL"
|
||||
val max = JavaClass.findMaxAndInvokeCallback({ a, b -> a.length - b.length }, "foo", "kotlin", { v = "OK" })
|
||||
val max = JavaClass.findMaxAndInvokeCallback({ a, b -> a.length() - b.length() }, "foo", "kotlin", { v = "OK" })
|
||||
if (max != "kotlin") return "Wrong max: $max"
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fun box(): String {
|
||||
val wc = WeirdComparator<String>()
|
||||
val result = wc.max({ a, b -> a.length - b.length }, "java", "kotlin")
|
||||
val result = wc.max({ a, b -> a.length() - b.length() }, "java", "kotlin")
|
||||
if (result != "kotlin") return "Wrong: $result"
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
fun box(): String {
|
||||
val result = WeirdComparator.max<String>({ a, b -> a.length - b.length }, "java", "kotlin")
|
||||
val result = WeirdComparator.max<String>({ a, b -> a.length() - b.length() }, "java", "kotlin")
|
||||
if (result != "kotlin") return "Wrong: $result"
|
||||
|
||||
val result2 = WeirdComparator.max2<String>({ a, b -> a.length - b.length }, "java", "kotlin")
|
||||
val result2 = WeirdComparator.max2<String>({ a, b -> a.length() - b.length() }, "java", "kotlin")
|
||||
if (result2 != "kotlin") return "Wrong: $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fun box(): String {
|
||||
val wc = WeirdComparator<String>().createInner()!!
|
||||
val result = wc.max({ a, b -> a.length - b.length }, "java", "kotlin")
|
||||
val result = wc.max({ a, b -> a.length() - b.length() }, "java", "kotlin")
|
||||
if (result != "kotlin") return "Wrong: $result"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
|
||||
fun test1(): Int {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo({(z: Int) -> "" + z}, 25, {String.() -> this.length})
|
||||
return inlineX.foo({(z: Int) -> "" + z}, 25, {String.() -> this.length()})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 2) return "test1: ${test1()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ package test
|
||||
public class Holder(var value: String = "") {
|
||||
|
||||
public fun plusAssign(s: String?) {
|
||||
if (value.length != 0) {
|
||||
if (value.length() != 0) {
|
||||
value += " -> "
|
||||
}
|
||||
value += s
|
||||
@@ -30,4 +30,4 @@ public inline fun <R> doCallWithException(h: Holder, block: ()-> R) : R {
|
||||
h += "inline fun finally"
|
||||
throw RuntimeException("fail");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ package test
|
||||
public class Holder(var value: String = "") {
|
||||
|
||||
public fun plusAssign(s: String?) {
|
||||
if (value.length != 0) {
|
||||
if (value.length() != 0) {
|
||||
value += " -> "
|
||||
}
|
||||
value += s
|
||||
@@ -30,4 +30,4 @@ public inline fun <R> doCallWithException(h: Holder, block: ()-> R) {
|
||||
h += "inline fun finally"
|
||||
throw RuntimeException("fail");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@ package test
|
||||
public class Holder(var value: String = "") {
|
||||
|
||||
public fun plusAssign(s: String?) {
|
||||
if (value.length != 0) {
|
||||
if (value.length() != 0) {
|
||||
value += " -> "
|
||||
}
|
||||
value += s
|
||||
@@ -33,4 +33,4 @@ public inline fun doCall(block: ()-> String, finallyBlock: ()-> String,
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user