Imports


#1

Here are two suggestions for imports.

1) It would be really cool to be have named package imports:

import com.pkg.whatever as whatev
...
val x = whatev.somePackageLevelFunction()

You can already do that with objects, but sometimes you don’t want to group your function within objects and have to use import com.pkg.whatever.WrapperObject.* a lot internally. Especially since auto-completion does not kick in.

2) Extension methods could use some import-level support.

It’s not rare that I want to import all extensions to a type X from a certain package. But I can’t do that, you either import everything (import com.pkg.*) or you must import your extension methods one by one.

You could put all extensions methods in a separate package (and it’s really nice that you don’t need to put them in a separate folder!), but again, internally you’ll need to use import com.pkg.xextensions.* a lot).

It could look something like this:

import com.pkg.(X) // import all extension methods for X defined in `com.pkg`

"Extension types" for Kotlin
#2

Aliases for packages

First paragraph look like very useful feature for developers how like write functional code.

For example, in big program clash on function signatures it’s very possible event.
In Clojure it solved by namespaces, but in Kotlin we has nothing (see below).

Alternatives:

  • “Object/Component object” - restrice to hold all functions in one file and have bad syntax
  • Hack with annotaion (see code) - even more strage syntax and not supports on JS/Native
@file:JvmName("Func")
fun's ....

//usage
typealias f = Func
f.fun1

In my case it’s will be the best to write something like:

import org.app.domain as d
import org.app.presenter as p

fun main (args: Array<String>) {
    d.func1()
    d.func2()
    p.func1()
    p.func2()
}

Maybe someone create proposal for KEEP?


#3

I don’t have the time to write one right now, but if someone does, I’m willing to review it.


#4

By the way Scala has package aliases:

import java.util.{concurrent => c}

val future = new c.CompletableFuture

If Kotlin wants to be best multi paradigm (FP) language, it’s +1 reason for aliases.

http://stackoverflow.com/a/28238600/7235355


#5

I agree that current mechanism where you can import function as with name change is pretty wired:/ and I would prefer import some package and name it

Currently it looks this way:

import func1.doSth as f1
import func2.doSth as f2

fun main(args: Array<String>) {
    f1()
    f2()
}

And when I am naming the function then I am doing it for reason. I don’t want anyone to use it with a different name. It is really confusing. In case of name conflict, I would prefer to prefix function with some name that will tell which function is that.


#6

I think it’s already supported:

import java.math.BigInteger as BigInt

fun main(args: Array<String>) {
	val a = BigInt("233")
	println(a + BigInt("666"))
}

When you’re confronted with some problems, RTFM first :smile:


#7

Named imports are possible for classes, not for packages.
Understand The Fucking Post first?


#8

Well, I apologize for my carelessness.
And I don’t have any idea about named imports for packages. :joy:

—Original—