2

I have a Kotlin enum class defined like this:

enum class EnumClass(val string: String) {

    VALUE_A(A), // [1]

    VALUE_B(B); // [2]

    companion object {

        const val A = "A"

        const val B = "B"
    }
}

and the compiler gives me following errors in lines [1] and [2]:

Variable 'A' must be initialized
Variable 'B' must be initialized

I can solve this error by extracting the consts to the top-level of source file but I don't like this solution. Is there any other way around this problem?

goshki
  • 120
  • 9

2 Answers2

4

I was able to get this to work by fully qualifying A and B:

enum class EnumClass(val string: String) {
    VALUE_A(EnumClass.A), 
    VALUE_B(EnumClass.B); 

    companion object {
        const val A = "A"
        const val B = "B"
    }
}
Todd
  • 30,472
  • 11
  • 81
  • 89
  • Thanks, it worked. What's strange is that after adding the qualifier Android Studio marks it as `Redundant qualifier name`... – goshki Oct 21 '19 at 12:59
1

This will no longer be allowed in 1.9. See this ticket.

enter image description here

Boken
  • 4,825
  • 10
  • 32
  • 42
David H
  • 11
  • 1