In C enums are all numeric and you can reference the value just by the name.
Example:
#include <stdio.h>
enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
int main()
{
enum week today;
today = wednesday;
printf("Day %d",today+1);
return 0;
}
Outputs: day 4
In Kotlin I would like something similar, at least being able to get rid of the .ordinal
.
Currently it's like this:
enum class Week { sunday, monday, tuesday, wednesday, thursday, friday, saturday }
and to access an element I have to use the verbose Week.monday.ordinal
Basically answer by @jrtapsell is great and full. But also in kotlin you can override invoke() operator.
enum class Weekday { MONDAY, TUESDAY;
operator fun invoke(): Int {
return ordinal
}
}
fun main(args: Array<String>) {
print("${Weekday.TUESDAY() + 1}")
}
Result: 2
AFM it is much prettier.
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