data class ApiBuyer ( var id: Int? = null, ...
# language-proposals
j
data class ApiBuyer ( var id: Int? = null, var customerId: String? = null, var firstName: String? = null, var lastName: String? = null, var addressLine1: String? = null, var addressLine2: String? = null, var city: String? = null, var state: String? = null, var postalCode: String? = null, var country: String? = null, var email: String? = null, var phone: String? = null, var password: String? = null, var status: String? = null, var tripIdPendingRating: Int? = null, var profileImageUrl: String? = null, var pushToken: String? = null, var rating: String? = null, var ratings: Int? = null, var isAdult: Boolean? = null, var defaultToApplePay: Boolean? = null, var applePayCardId: Boolean? = null, var defaultTipPercent: Int? = null, var promoCode: String? = null, var tosAcceptedVersion: String? = null, var createdAt: Date? = null, var updatedAt: Date? = null, var version: Int? = null ) { override fun equals(other: Any?): Boolean { return super.equals(other) } override fun hashCode(): Int { return super.hashCode() } }
😱 6
l
This snippet is nonsense. You don't need to override
equals
and
hashcode
âž• 5
j
yes you do or you get the default implementation which is harmful
l
I think you didn't understand the point of data classes. The equals and hashcode functions are implemented by the kotlin compiler, and it generates valid hashCode and equals functions taking into account all the properties of the object. Sure lombok does the same with more configuration, but JetBrains wanted to keep it simple and releasable at first
m
This snippet is nonsense, because if you do override equals and hashCode you don't call super.
And sometimes it might be reasonable to override, e.g. when you want to have equality based only on id.
IDE also suggest to override when you have array in data class.
m
Also keep in mind that
super.hashCode()
is Object’s one, not data class’s.
m
Oh... Then that code "makes" sense.
j
its intentionally objects one
because the generated one is a harm
Also you cant use equality based on id with hibernate
So generating this stuff as kotlin is doing is only going to cause people trouble anyway
And lombok users are not progressive java users in any quantity
l
@johnl Who uses Java's serialization today?
i
@johnl So why do you make this class
data
if you do not use its capabilities, like generated
equals
and
hashCode
?
l
@johnl There are cross-language serialization solutions such as Protobuf, Flatbuffers, Cap'N Proto, or databases that don't rely on an in-memory (understand RAM) representation and logic. Java serialization wasn't a good idea because of how painful is it to handle backwards and forward compatibility. There's no reason
data classes
would target serialization