But this is not an assumption that can be made by the standard library. There are many reasons why people don’t like java Object.clone
. Even the documentation of it states that you can’t make any assumptions about it’s result. Not even simple statements like x.clone() == x
or x.clone().getClass == x.getClass()
can be assumed to always be true. All that is supposed to be guaranteed is that x.clone() !== x
, which means the function is practically useless for any generic usage. Thankfully kotlin didn’t make the same mistake and removed this function form Any
.
There might be a point in discussing adding a Cloneable
interface to kotlin and deciding on a strict definitions what this interface should mean. Otherwise there is no way to create a deepCopy
. The problem I see with this is that the best definition of Cloneable
probably depends on the project and therefor can not be made by the standard library.