``` fun a() = 1 class A() { fun a() = 2 ...
# announcements
m
Copy code
fun a() = 1

class A() {
    fun a() = 2

    fun printA() {
        println((::a)())
    }
}

fun main(args: Array<String>) {
    A().printA() // Prints: 2
}
Is there a way I can reference top-level function instead?
stackoverflow 1
h
If there is, I don't know it. I'd suggest putting the question on SO.
k
Maybe if you used the fully-qualified (FQ) package name? You can use the FQ name to disambiguate between the two when you just want to invoke the method. But I'm not sure about getting a method reference to the top-level one with the FQ name
s
If you do not need the function reference, you can invoke the toplevel
a()
by using the package name:
println(full.package.name.a())
d
I don't think there is a way to do it except "obvious" ones like the one above or importing toplevel function
as
function with another name. I wish something like
println(@file::a)
worked (at least it'll be consistent with syntax for file annotations
@file:JvmName("Foo")
).
It might be worth a youtrack issue, I'd vote for it 🙂
m
@kevinmost @s1m0nw1 @Dmitry Kandalov If they were in different packages then I can use import alias:
Copy code
// File1
package aaa

fun a() = 1

class A() {
    fun a() = 2
}

// File2

import aaa.A
import aaa.a as b

fun A.aaa() {
    println((::a)())
    println((::b)())
}

fun main(args: Array<String>) {
    A().aaa()
}
I wanted to know if there is a way to explicitly reference top-level function. I think there should be because there can be cases when we want to make explicit call. Something like
top::a
and
top.a()
(or any other keyword. I just made it up).
d
You can use import alias even in the same file, package.
s
m
@Dmitry Kandalov Indeed. It looks like an answer 😄