Files
kotlin-fork/compiler/testData/codegen/box/delegatedProperty/propertyMetadataShouldBeCached.kt
T
Alexander Udalov a7b88e9485 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
2014-11-27 20:38:17 +03:00

47 lines
1.1 KiB
Kotlin

import java.util.IdentityHashMap
class A {
var foo: Int by IntHandler
class object {
var bar: Any? by AnyHandler
}
}
val baz: String by StringHandler
val metadatas = IdentityHashMap<PropertyMetadata, Unit>()
fun record(p: PropertyMetadata) = metadatas.put(p, Unit)
object IntHandler {
fun get(t: Any?, p: PropertyMetadata): Int { record(p); return 42 }
fun set(t: Any?, p: PropertyMetadata, value: Int) { record(p) }
}
object AnyHandler {
fun get(t: Any?, p: PropertyMetadata): Any? { record(p); return 3.14 }
fun set(t: Any?, p: PropertyMetadata, value: Any?) { record(p) }
}
object StringHandler {
fun get(t: Any?, p: PropertyMetadata): String { record(p); return p.name }
fun set(t: Any?, p: PropertyMetadata, value: String) { record(p) }
}
fun box(): String {
val a = A()
a.foo = 42
a.foo = a.foo + baz.length()
a.foo = 239
A.bar = baz + a.foo
baz + A.bar
if (metadatas.keySet().size() != 3)
return "Fail: only three instances of PropertyMetadata should have been created\n${metadatas.keySet()}"
return "OK"
}