https://kotlinlang.org logo
#intellij
Title
# intellij
b

bod

10/06/2023, 3:49 PM
What's the rationale for this suggestion?
4
e

ephemient

10/06/2023, 3:53 PM
similar to the suggestion for updating
class
to
data class
I guess
b

bod

10/06/2023, 3:54 PM
similar to the suggestion for updating
class
to
data class
I guess
I don't get such suggestion. It seems the object one is related to the fact that it's inside a sealed interface
1
k

Klitos Kyriacou

10/06/2023, 4:29 PM
Perhaps it's related to this recent discussion. Sealed objects are often used in exhaustive
when
clauses, but if the object comes from Java serialization, or from a mocking framework, the object may not be an actual singleton and the
==
operator used by the
when
clause may return false even for objects of the same type. Making it a
data object
fixes this possibility.
1
👍 1
b

bod

10/06/2023, 4:32 PM
hmm interesting!
c

Casey Brooks

10/06/2023, 4:36 PM
Also, the default
toString()
of a non-data object is platform-dependent while
toString()
of a data class is not. This can be problematic in KMP apps for things like logging, when the platform’s representation doesn’t include any type information. For example, see how the
toString()
here changes between JVM and JS targets (and how completely useless the JS version is) https://pl.kotl.in/OZ8JtYfeC
b

bod

10/06/2023, 4:40 PM
I'm now wondering why
data object
exists and
object
is not doing "the right thing" by default 🙂 But I suppose I'm not seeing the full picture (maybe that would be a breaking change...)
c

Casey Brooks

10/06/2023, 4:44 PM
Many top-level objects don’t need all the extra stuff generated by the
data object
, and it would increase the binary size significantly to make them all data objects. A normal object is simple a singleton class with no extra ceremony. It’s just that the common semantics of an object inside a sealed class is closer to that of a data class that doesn’t have members, rather than an actual singleton, hence the addition of
data object
and its Intellij inspection
b

bod

10/06/2023, 4:46 PM
that makes a lot of sense! Thanks 🙏