jjjmm
jjjmm

Reputation: 51

Is it possible to name an enum constant "name" in 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
}

Upvotes: 5

Views: 6579

Answers (1)

Cililing
Cililing

Reputation: 4773

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

Upvotes: 1

Related Questions