如何在Kotlin中编写一个包级静态初始化程序?

前面的问题显示了如何使用其companion object在类中放置静态初始化程序。 我试图找到在包级别添加静态初始值设定项的方法,但似乎包没有伴随对象。

 // compiler error: Modifier 'companion' is not applicable inside 'file' companion object { init { println("Loaded!") } } fun main(args: Array<String>) { println("run!") } 

我已经尝试了其他可能有意义的变体( init是独立的, static ),我知道作为一个解决方法,我可以使用一次性val作为

 val static_init = { println("ugly workaround") }() 

但有没有一个干净的,官方的方式来实现相同的结果?


编辑:正如@ mfulton26的回答所提到的,在JVM中没有真正的包级函数。 在幕后,kotlin编译器正在封装所有的免费函数,包括一个类中的main函数 。 我试图添加一个静态初始化到这个类 – 由kotlin为文件中声明的自由函数生成的类。

目前无法将代码添加到为Kotlin文件类生成的静态构造函数中,只有顶级属性初始值设定项才会到达此处。 这听起来像一个功能请求,所以现在有一个问题来跟踪: KT-13486包级“初始化”块

另一个解决方法是将初始化置于顶级私有/内部对象中,并在那些依赖于初始化效果的函数中引用该对象。 当第一次被引用时,对象会被懒惰地初始化。

 fun dependsOnState(arg: Int) = State.run { arg + value } private object State { val value: Int init { value = 42 println("State was initialized") } } 

正如你所提到的,你需要一个可以在初始化时运行的属性:

 val x = run { println("The package class has loaded") } 

我通过在Kotlin文件下使用顶层的Backing Property来解决这个问题。 Kotlin文档:支持属性

 private var _table: Map<String, Int>? = null public val table: Map<String, Int> get() { if (_table == null) { _table = HashMap() // Type parameters are inferred // .... some other initialising code here } return _table ?: throw AssertionError("Set to null by another thread") }