https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jason5lee

08/27/2021, 6:33 AM
I’d like to define some types or functions in
a.b.c.d
and in another place I can
import a.b.c.d
and reference them as `d.TypeName`/`d.function`. Is there a better way other than defining the inner
d
as an object and put everything inside, which I’m not sure whether it is a good practice.
s

Stephan Schroeder

08/30/2021, 6:40 AM
sounds like "named imports"/"import alias" to me, see https://medium.com/@erickpranata/kotlin-import-alias-3f2831295902 At least it's the closest thing to what you ask for. Well, at least as long as
d
is a package. If
d
is a singleton/object, you'd retain your wish to write
d.something()
Copy code
object D {
  fun something(): Unit {}
}

// or put it in the companion object of a normal class
class D {
  companion object {
    fun something(): Unit {}
  }
}
In the future Kotlin will probably get namespaces, which will probably match what you ask for even more:

https://www.youtube.com/watch?v=0FF19HJDqMo&t=656s

3 Views