and to begin with, how do I even write the express...
# getting-started
j
and to begin with, how do I even write the expression to init the enum class?
Copy code
Color("red")
does not work because
enum types cannot be instantiated
. And
Copy code
Color.valueof("red")
throws me an
illegal argument exception
a
jasonlow: you should use the same string as the constant name
Color.valueOf("RED")
another issue is val name clashes with enum property named "name"
you can use something like
enum class Color(val desc: String) { RED("red"), GREEN("green"), BLUE("blue"); companion object { fun byDesc(desc: String) = values().firstOrNull { it.desc == desc } } }
if you need a different name, not the same as the constant declaration
and then you can
Copy code
Color.byDesc("red")