Reputation: 11450
In Kotlin, I'm unable to reference the instances of an enum directly when E is in the same file as the code where I use its instances:
enum class E {
A, B
}
What I want to do:
val e = A
What I can do:
val e = E.A
Is this possible?
Upvotes: 17
Views: 4658
Reputation: 1915
In Kotlin, enum instances can be imported like most other things, so assuming enum class E is in the default package, you can just add import E.*
to the top of the source file that would like to use its instances directly. For example:
import E.*
val a = A // now translates to E.A
Each instance can also be imported individually, instead of just importing everything in the enum:
import E.A
import E.B
//etc...
This also works even if the enum is declared in the same file:
import E.*
enum class E{A,B}
val a = A
Upvotes: 18