Kotlin顶级功能范围&阴影 [英] Kotlin top-level function scopes & shadowing

查看:153
本文介绍了Kotlin顶级功能范围&阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  package CoolWithATwist 

/ /代码在线性时间内解决TSP,然后是:

fun< T> println(x:T){
kotlin.io.println(x)
haltAndCatchFire()//或任何烦人/破坏性函数
}

如果包是以字节码形式分发的,我是否正确地假设Kotlin的默认导入标准库模块的规则是根据文档,随后导入另一个模块,例如CoolWithATwist,实际上会影响标准库自动包含的println函数和因此,如果用户真正调用println,上面的代码会执行吗?



检测它的最好方法是什么,因为Kotlin编译器没有警告全局函数的阴影或关于明确命名你实际调用的是哪一个函数,IntelliJ Idea中的Kotlin插件(从版本1.1.3开始),还是就我的知识而言,Android Studio对此有何评论?

解决方案

假设您在源文件夹中有以下类:

  kotlin 
|
| ---- CoolWithATwist
| |
| | --- function.kt其中包含您自己的println()函数
| |
| | --- test1.kt(无进口)
| |
| | --- test2.kt(import kotlin.io.println)
| |
| | --- test.kt(import kotlin.io。*)
| |
| | ___ NestedPackage
| |
| | ___ test3.kt(无进口)
|
| ____ main.kt

main.kt test2.kt test3.kt 将使用 kotlin.io .println 直接。



test1.kt 将使用包顶层函数 println



test.kt 将会使用包装顶级函数 println ,因为星形导入语句优先级低于包顶级范围。



这意味着kotlin中的函数find策略不会冒泡,只能在本身的package中找到顶级函数。并且查找策略顺序为: local > 封闭> 函数 class > 脚本> 导入语句> 顶级包> 明星导入语句> kotlin顶级



您可以简单地使用 CTRL + B / CTRL + ALT + B / F4 ,然后您可以跳转到函数实际调用的源代码,例如:

  fun foo(){
println(bar);
// ^ ---在此处移动光标并按下CTRL + B / CTRL + ALT + B / F4


Let's say I write 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
}

Should the package be distributed in bytecode form, am I correct in assuming that Kotlin's rules on default importing of standard library modules as per the documentation and that subsequently importing another module such as CoolWithATwist will in fact shadow the standard library auto-included println function and thus the above code will execute should the user actually call println?

What is the best way of detecting this since Kotlin compiler doesn't warn about shadowing global functions or about having to explicitly name which function you're actually calling, nor does the Kotlin plugin on IntelliJ Idea (as of version 1.1.3) or, to the best of my Knowledge, Android Studio, say anything about it?

解决方案

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 

the main.kt, test2.kt and test3.kt will using kotlin.io.println directly.

the test1.kt will using the package top-level function println.

the test.kt will using the package top-level function println since the star import statement priority is lower than package top-level scope.

which means the function find strategy in kotlin is not bubbled, only find the top-level function in itself package. and the find strategy order is: local > enclosing > function > class > script > import statement > package top-level > star import statement > kotlin top-level.

you can simply using CTRL+B/CTRL+ALT+B/F4 at the call-site function, then you can jump to the source code which the function was actually called,for example:

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

这篇关于Kotlin顶级功能范围&amp;阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆