fix: Implementation for Equals & Hashcode in Builder
Context
When we compare the builder, like EmbedBuilder, the comparison between two instances will always return false no matter the field set. For example:
println(EmbedBuilder() == EmbedBuilder()) // false
println(EmbedBuilder().apply { title = "title" } == EmbedBuilder().apply { title = "title" }) // false
println(EmbedBuilder().hashCode()) // 2096057945
println(EmbedBuilder().hashCode()) // 1689843956
This behavior seems to be a bug and needs to be fixed because according to the result, equals & hashcode methods don't work.
Usage
In my bot, when I render my components, I would like to compare the last InteractionResponseModifyBuilder and the new from the rendering to send only the changed components (embed / actionRow / text) to the discord API and consequently reduce the network load of the bot
After change
If we try the previous code in this PR, this is the result:
println(EmbedBuilder() == EmbedBuilder()) // true
println(EmbedBuilder().apply { title = "title" } == EmbedBuilder().apply { title = "title" }) // true
println(EmbedBuilder().apply { title = "title"; description = "description" } == EmbedBuilder().apply { title = "title" }) // false
println(EmbedBuilder().hashCode()) // 1
println(EmbedBuilder().hashCode()) // 1
The result makes more sense
As already discussed on Discord I don't really feel like this is the right solution. Kord's builder classes are intended to be short-lived, mutable (!) objects that help to create immutable requests and closely model fields of the Discord API (e.g. optional vs. nullable).
Having a data model specific to your domain logic seems to be a way more robust solution to your usecase.
I'm also not aware that implementing equals and hashCode is frequently done for similar builder classes in other projects.
Understood. I thought that could be useful for several projects (like Kordex, I think they keep the builders to compare in a next time) but for my need, I can create my own builders, that allows me to have more flexibility than using Kord builder
Nevertheless, thank you for your work :)