So, basically I would like the field to be a String, but restricting it only to one of these 5
🧵 2
e
Eric Grimsborn
08/07/2020, 11:40 AM
Maybe like this:
Copy code
enum class FRUIT {
APPLE,
ORANGE,
PEAR,
BANANA,
PEACH
}
val apple: FRUIT = FRUIT.APPLE
val appleStr: String = apple.name // "APPLE"
val appleStr2 = "$apple" // "APPLE"
if you need different casing (or a different type) stored in enum:
Copy code
enum class FRUIT(val lowerCase: String) {
APPLE("apple"),
ORANGE("orange"),
PEAR("pear"),
BANANA("banana"),
PEACH("peach")
}
val apple: FRUIT = FRUIT.APPLE
val appleStr: String = apple.lowerCase // "apple"
val appleStr2 = "$apple" // "APPLE"
d
Daniele B
08/07/2020, 11:54 AM
@Eric Grimsborn fantastic!!! I didn’t know with enum you can access the name property!