mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 15:53:19 +00:00
Introduce kotlin.Cloneable
- Cloneable is a trait with a single protected member 'clone', which is mapped to java.lang.Cloneable on JVM - 'clone' is non-abstract to be able to call 'super.clone()' in the implementations. Also if you need your class to be Cloneable, most of the time inheriting from Cloneable and calling 'super.clone()' will work - hack 'super.clone()' in JVM intrinsics and TImpl delegation generation - make arrays Cloneable, handle 'clone()' calls in the intrinsic #KT-4890 Fixed
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A = A(x)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (b != a) return "Fail equals"
|
||||
if (b identityEquals a) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A = super.clone() as A
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A {
|
||||
val result = super.clone() as A
|
||||
result.x = 239
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (a == b) return "Fail: $a == $b"
|
||||
if (a identityEquals b) return "Fail: $a identityEquals $b"
|
||||
if (b.x != 239) return "Fail: b.x = ${b.x}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.HashSet
|
||||
|
||||
fun box(): String {
|
||||
val a = HashSet<String>()
|
||||
a.add("live")
|
||||
a.add("long")
|
||||
a.add("prosper")
|
||||
val b = a.clone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
open class A : Cloneable {
|
||||
public override fun clone(): A = super.clone() as A
|
||||
}
|
||||
|
||||
open class B(var s: String) : A() {
|
||||
override fun clone(): B = super.clone() as B
|
||||
}
|
||||
|
||||
open class C(s: String, var l: ArrayList<Any>): B(s) {
|
||||
override fun clone(): C {
|
||||
val result = super.clone() as C
|
||||
result.l = l.clone() as ArrayList<Any>
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val l = ArrayList<Any>()
|
||||
l.add(true)
|
||||
|
||||
val c = C("OK", l)
|
||||
val d = c.clone()
|
||||
|
||||
if (c.s != d.s) return "Fail s: ${d.s}"
|
||||
if (c.l != d.l) return "Fail l: ${d.l}"
|
||||
if (c.l identityEquals d.l) return "Fail list identity"
|
||||
if (c identityEquals d) return "Fail identity"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
data class A(val s: String) : Cloneable {
|
||||
fun externalClone(): A = clone() as A
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A("OK")
|
||||
val b = a.externalClone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return b.s
|
||||
}
|
||||
Reference in New Issue
Block a user