How do I filter by an enum class in kotlin? (just learning)
In the code below the enum class defined earlier in the file is PayStatus{PAID,UNPAID}
.
fun nextRentDate(): LocalDate? {
return rentPaymentSchedule.
filter { it.value.paymentStatus is PayStatus.UNPAID}.
minBy { it.value.date.toEpochDay() }?.value?.date
}
I get the error: Kotlin:
Incompatible types: PayStatus.UNPAID and Enum
To get an enum constant by its string value, you can use the function valueOf() on the Enum class.
Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.
You need to use the class name as a prefix even within the same package unless you import the enum. (The enum is not a member of the package; only of the enclosing class.) If the enum itself is explicitly imported, the prefix is unnecessary even when in another package.
You must use the ==
operator when checking for enum values !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With