How to access a shadowed file-level property in the src package

val a = "Hi"

fun main() {
    val a = 5
    println(a)
}

Assuming I put this directly into a kt file in the src package. How do I access the top-level a inside the function?

You can access it using it’s complete qualified name. So if your package is foo.bar you can use foo.bar.a.

Thanks. But I am specifically asking about the src package. Also, is this considered something I should avoid because it looks very weird in the same file?

There is no such thing as src package. It is called default package. And you can do what you want via named imports like this:

import a as rootA
val a = "Hi"

fun main() {
    val a = 5
    println(rootA)
}

Still, I would not recommend to use default packages.

Thank you, this clarified my question. Is this importing of a top-level property within the same file considered a bad style? It looks very werid. It’s just a theoretical question.

Sometimes you have to do imports from the same file. For example it you have constants in one class and want to use them in another. In your particular example I think the problem is with shadowing, not with imports. You can always change the name of local variable. Also please remember that top level values are usually not the best practice.

Yes, probably. Just like shadowing names or using the default package.