Why Kotlin 'Any' class do not have clone() method?

Java has clone() method in its base class(Object) of every class.
Kotlin don’t have clone() method in Any class which is base for every class.

I want to understand the reason for not adding this method from design point of view.
Was there any problem in java from design perspective while supporting this method ?

Maybe there is no clone method, because Any does not only represent object (reference types) but also value types like Int, Boolean and so on. It wouldn’t make much sense to clone value types since they are copied anyway when passed around.

More likely it’s just that clone in java is super unreliable. There is no sure way of checking if a class actually implements clone. That’s why classes that implement clone should always extend Cloneable as well.

3 Likes

Mmm, clone() in Java is just badly-designed.

Most relevantly: it’s protected — not publicly-accessible — in Object anyway. It also bypasses any constructors or initialisers (and the superclass constructor chain), which can have security implications; without great care, it breaks immutability and singletons; and it doesn’t work well with subclasses. The use of a no-method marker interface Cloneable is misleading in itself: interfaces should say something to users of the class, but this one instead modifies the behaviour of a protected method in the superclass.

It’s arguably a good thing that Kotlin doesn’t expose it.

6 Likes

Indeed. The reason is that clone is badly designed.

2 Likes

Thanks to all :slight_smile: @elizarov @Wasabi375 @gidds @medium