I would like my Enum values to be comparable with other types, for instance, String. I am not entirely sure why it complains and what the error means.
enum class Fruits(val value: String): Comparable<String>{
Apple("apple"),
Orange("orange");
override fun compareTo(other: String): Int {
return compareValuesBy(this.value, other)
}
}
val test: Boolean = Fruits.Apple == "apple"
Error says:
Type parameter T of 'Comparable' has inconsistent values: Fruits, String
compareTois final inEnumso you may have to rethink your approach. The other one is that you needComparable<Fruits>rather thenComparable<String>==. That's managed by theequalsfunction. But the contract forequalsis that it must be reflexive. Since you cannot modify theString.equals()function, you cannot safely do this.