Does anyone know if there is a way to import all the members of an enum which is in the same file as where you want to use them?
I want to do this:
Copy code
// myfile.kt
enum MyEnum {
HELLO, GOODBYE, HEY, GREETINGS
}
fun myFunction() {
doSomethingWith(HELLO) // instead of MyEnum.HELLO
doSomethingElse(GOODBYE) // instead of MyEnum.GOODBYE
}
I'd normally use
MyEnum.HELLO
and
MyEnum.GOODBYE
but I have a use case where that causes more harm than anything else.
The current workaround I use is to simply declare
MyEnum
in a separate file, but that is quite annoying.
(please redirect me to the right channel where I can ask this if there's a better place for this question!)
m
Milan Hruban
02/02/2020, 4:33 PM
You should just be able to do
Copy code
import package.MyEnum.*
☝️ 3
m
Mark Murphy
02/02/2020, 4:34 PM
Copy code
import MyEnum.*
enum class MyEnum {
HELLO, GOODBYE, HEY, GREETINGS
}
fun myFunction() {
println(HELLO)
println(GOODBYE)
}
fun main() {
myFunction()
}
In this case, my code is in the top-level package, so I did not need to put a package qualifier on
MyEnum
. In a more traditional case, you would provide the fully-qualified class name for
MyEnum
in the
import
.
m
Matthieu Stombellini
02/02/2020, 4:34 PM
I didn't realize you could import something that's from the same file 🤦♂️
Thank you very much everyone!