Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to name an enum constant "name" in Kotlin?

Tags:

enums

kotlin

Is there any workaround to make it possible to name a Kotlin enum constant name?

This works in Java:

public enum Dummy {
    name
}

This throws Conflicting declarations: enum entry name, public final val name: String in Kotlin

enum class Dummy {
    name
}
like image 754
jjjmm Avatar asked Nov 07 '22 10:11

jjjmm


1 Answers

You can't do it. Every enum member has two properties: name (string) and ordinal (int). So, there is conflict with names.

And remember. Even if you could do that you should not. Enums should be UPPERCASE (my mistake, can be also CamelCase, check first comment) and breaking this rule can be very distracting for other developers working with your code.

More information in Kotlin docs: https://kotlinlang.org/docs/reference/enum-classes.html

like image 87
Cililing Avatar answered Dec 07 '22 21:12

Cililing