Stumbled across a fun bug today: <https://youtrack...
# getting-started
r
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
Examples in the ticket show a class not a data object?
f
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
the ticket refers to the new
data object
construct that doesn’t support
copy
as objects are singletons.
f
yes, a
data object
is just a singleton so a copy does't make sense.
c
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
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
it's interesting that the error is the same as for data classes, but the decompiled bytecode does not have a
copy
method
c
exactly.
likely somewhere in the compiler theres something shared between data objects and data classes that gets this wrong.
r
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
yea, it shouldn’t be possible for syntactically valid Kotlin to crash the compiler. bug somewhere.