Suppose I have some Utils.kt file which will contain only some utility functions, no classes, no objects. And suppose that those functions use some common set of constant values.
所以我做了这样的事情:
package myapp
private val CONST1 = 1
private val CONST2 = 2
public fun function1() {}
public fun function2() {}
Unfortunately Kotlin treats private as "available to the whole package". So CONST1 and CONST2 are available to all files which are in the same package. The only way to isolate them is to move this file to a separate package.
但是,如果我有几个类似这样的实用程序文件,每个文件都有自己的private个常量集,会怎么样呢?那么我只有两个 Select :将它们中的每一个都移动到一个唯一的包中,或者放弃,让它们的const可以在任何地方访问.
Either way seems to create clutter.
有什么 Select 或建议吗?
(实际上,我必须说这是我在Kotlin中为数不多的困扰我的事情之一--没有办法使某个实体是文件本地的(不使用一些语法技巧):它要么对整个包可用,要么对所有人都可用.)
UPD:这个问题现在已经过时了(见公认的答案)