Is there a way to nest an enum within a data class in Kotlin?
data class D(val a:Any) {
enum class E {F,G}
...
}
Or to declare it inline within a function?
fun foo() {
enum class E {F,G}
doSomething()
}
I can't find documentation on the rules for where enums are allowed to be declared.
Yes, you can nest the enum in a data class, but not in a function:
data class Outer(val a: InnerEnum) {
enum class InnerEnum { A, B }
}
fun foo() {
val o = Outer(Outer.InnerEnum.A)
println(o) // --> Outer(a=A)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With