https://kotlinlang.org logo
r

Ruckus

07/11/2023, 9:59 PM
Stumbled across a fun bug today: https://youtrack.jetbrains.com/issue/KT-60370/data-objects-cannot-have-a-function-named-copy Is the
copy
function supposed to be reserved somehow for data objects? There's nothing about it in the 1.9.0 release notes, and the 1.8.20 release note say
Because a
data object
declaration is intended to be used as a singleton object, no
copy()
function is generated.
c

Chris Lee

07/11/2023, 10:08 PM
Examples in the ticket show a class not a data object?
f

Francesc

07/11/2023, 11:48 PM
when you create a data class, the compiler generates
copy
methods with default arguments. If you then define your own copy method, it won't compile because the method is already defined. It's not reserved, it's just that the method is automatically generated and your own one clashes with it
c

Chris Lee

07/11/2023, 11:48 PM
the ticket refers to the new
data object
construct that doesn’t support
copy
as objects are singletons.
f

Francesc

07/11/2023, 11:49 PM
yes, a
data object
is just a singleton so a copy does't make sense.
c

Chris Lee

07/11/2023, 11:50 PM
correct. so if there is no copy method generated, why does Kotlin prevent providing one named that? (regardless of what that method does or whether its appropriate)
r

Ruckus

07/11/2023, 11:51 PM
Sorry, that was a copy/paste error on my part. Those should all be `data object`s, not `class`es. Fixed in the ticket.
👍 2
f

Francesc

07/11/2023, 11:53 PM
it's interesting that the error is the same as for data classes, but the decompiled bytecode does not have a
copy
method
c

Chris Lee

07/11/2023, 11:53 PM
exactly.
likely somewhere in the compiler theres something shared between data objects and data classes that gets this wrong.
r

Ruckus

07/11/2023, 11:59 PM
On a data class, you can define a copy method with a different signature than the constructor without a problem. You will only see the conflicting overload error when they match. The compiler failing entirely in the second example seems unique to data objects.
c

Chris Lee

07/11/2023, 11:59 PM
yea, it shouldn’t be possible for syntactically valid Kotlin to crash the compiler. bug somewhere.