Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by enum class property Kotlin

Tags:

enums

kotlin

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

like image 841
mleafer Avatar asked Aug 16 '17 16:08

mleafer


People also ask

How do I get the enum class value in Kotlin?

To get an enum constant by its string value, you can use the function valueOf() on the Enum class.

Can enum class have methods Kotlin?

Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.

How do I view enum outside class?

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.


1 Answers

You must use the == operator when checking for enum values !

like image 56
Stefano.Maffullo Avatar answered Sep 20 '22 00:09

Stefano.Maffullo