Hi. A question about function references. Say I have two packages, `foo` and `bar`. in `bar.Baz.kt`...
j
Hi. A question about function references. Say I have two packages,
foo
and
bar
. in
bar.Baz.kt
I have this:
Copy code
package bar

fun baz(i: Int) = i + 1
and in
foo.Foo.kt
this:
Copy code
package foo

import bar.baz

fun xd(op: (Int) -> Int) {
    println("calling $op with param 4 and getting ${op(4)}")
}

fun main() {
    xd(::baz)
}
everything works great. now I want to get rid of
import bar.baz
. How do I do that?
xd(::bar.baz)
does not compile. What’s the syntax here?
r
If I'm not mistaken (which is an easy thing to happen)... It's impossible to use a function (or anything) from another package without it being imported. If I'm incorrect, somebody please correct me, but that's my understanding.
j
It's actually incorrect. You can use fully qualified names for almost everything actually
j
so what would be the syntax for it, @Joffrey? without using
import
(and of course without switching to lambda)
j
Unfortunately, I said "almost everything" and I believe qualified references to top-level functions are not supported at the moment: https://youtrack.jetbrains.com/issue/KT-52701/Support-fully-qualified-callable-reference-to-constructor-top-level-function#focus=Comments-27-6176973.0-0
🙇 1
r
If you're getting a conflict you can alias one of them:
Copy code
import bar.baz as baz
import bar2.baz as bar2baz

fun main() {
    xd(::baz)
    xd(::bar2baz)
}
j
yes, I’m afraid imports are necessary after all, and with conflicts, also with aliases :(