Can I have overridden `val` fields in an enum? So...
# getting-started
c
Can I have overridden
val
fields in an enum? Something like this? Or do I have to refactor them into getters?
Copy code
enum class Foo {
    abstract val bar: String
    FIRST { override val bar = "FIRST BAR" }
    SECOND { override val bar = "SECOND BAR" }
}
s
is there a specific reason you can’t have the value in the constructor?
Copy code
enum class Foo(val bar: String) {
  FIRST("FIRST BAR"),
  SECOND("SECOND BAR"),
  ;
}
c
no, that's a great idea; I'll do that. More generally, just because I'm curious, is there a reason my first example won't work? Maybe something dumb I'm doing with the syntax?
d
Yes, put the abstract property after the
;
which follows the enum constants.
👆 1
r
Copy code
enum class Foo {
    FIRST { override val bar = "FIRST BAR" },
    SECOND { override val bar = "SECOND BAR" };
    abstract val bar: String
}
c
Sweet, thank you!