Skip to content
Advertisement

Kotlin: Cannot import-on-demand from object

I am trying to make a library where you can call functions of an object through “static imports” (import-on-demand in Kotlin). Unfortunately Kotlin seems to have a limitation as seen below.

enter image description here

Why is this a limitation? Is there anything I can do to get around it? (I’ve tried using a class with a companion object and in that case the functions don’t even resolve.)

Advertisement

Answer

Although you cannot import package.KotlinObject.* from a Kotlin object directly, you can import package.*. To avoid unnecessary or ambiguous imports, simply create a new package, declare the classes, variables or extensions you wish to import inside a raw .kt file and then import-on-demand from the parent package.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement