Reputation: 60081
I have my enum as below
enum class ImagesType(val descriptor: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge")
}
println(ImagesType.BIGGER)
it will print BIGGER
val x = ImagesType.valueOf("SMALLER")
, it will get val x = SMALLER
This is because the name
is the same as the enum characters. I am hoping to override the name
with the description
, where the following will be true instead
println(ImagesType.BIGGER)
it will print Bigger Image - Fall
val x = ImagesType.valueOf("Smaller Image - Lion")
, it will get val x = SMALLER
I tried
enum class ImagesType(override val name: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge")
}
But it fails stating that name
is final.
Upvotes: 4
Views: 7565
Reputation: 60081
I use way to get what I wanted.
enum class ImagesType(val descriptor: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge");
override fun toString(): String {
return descriptor
}
companion object {
fun getEnum(value: String): ImagesType {
return values().first { it.descriptor == value }
}
}
}
So it get the result I need
println(ImagesType.BIGGER)
it will print Bigger Image - Fall
x = ImagesType.getEnum("Smaller Image - Lion")
, it will get val x = SMALLER
I kind of workaround of overriding valueOf
by replacing it with getEnum
. Looks like valueOf
can't be overriden.
Upvotes: 8