Kotlin Top Level Scope

Let's say I'm writing a Kotlin package containing the following code:

package CoolWithATwist

// code that solves the TSP in linear time followed by this:

fun <T> println(x: T) {
    kotlin.io.println(x)
    haltAndCatchFire()  // or any annoying/destructive function
}

If the package will be distributed in the form of byte code, can I start with the assumption that the Kotlin manages the standard import standard library modules default documents , and that subsequently another module imports, such as CoolWithATwist, actually obscures the automatic printing function included standard library, and so the above code will be executed if the user really calls println?

What is the best way to detect this, since the Kotlin compiler does not warn you to obscure global functions, or to explicitly indicate which function you are actually calling, and the Kotlin plugin on IntelliJ Idea (since version 1.1. 3), or, as far as I know, Android Studio, tell me something about this?

+4
source share
1 answer

let's say you have the following classes in your source folders:

kotlin
|
|---- CoolWithATwist
|        |
|        |--- function.kt which contains your own println() function
|        |
|        |--- test1.kt (no imports)
|        |
|        |--- test2.kt (import kotlin.io.println)
|        |
|        |--- test.kt (import kotlin.io.*)
|        |
|        |___ NestedPackage
|                   |
|                   |___ test3.kt (no imports)
|
|____ main.kt 

main.kt, test2.ktand test3.ktwill be directly used kotlin.io.println.

test1.ktwill use the top-level function of the package println.

test.ktwill use the top-level function of the package println, since the priority of the star import operator is lower than the top-level area of ​​the package.

, , . : local > enclosing > function > class > script > import statement > package top-level > star import statement > kotlin top-level.

CTRL+B/CTRL+ALT+B/F4 , , , :

fun foo(){
   println("bar");
   // ^--- move the cursor here and press `CTRL+B`/`CTRL+ALT+B`/`F4`
}
+5

Source: https://habr.com/ru/post/1680742/


All Articles