kts脚本里如何使用反射实验特性


#1

代码如下
`import kotlin.reflect.typeOf

@kotlin.ExperimentalStdlibApi
fun func1(){
var mm = typeOf<<Map<String, String>>()
println(mm)
}

func1()
`

报错error: this declaration is experimental and its usage must be marked with ‘@kotlin.ExperimentalStdlibApi’ or ‘@OptIn(kotlin.ExperimentalStdlibApi::class)’ (Test.kts:12:1)
Test.kts:12:1: error: this declaration is experimental and its usage must be marked with ‘@kotlin.ExperimentalStdlibApi’ or ‘@OptIn(kotlin.ExperimentalStdlibApi::class)’


#2

试一下在文件最上面写 @file:kotlin.ExperimentalStdlibApi


#3

不行,写在最上面,import就失效了


#4

只能在kt文件的main函数里搞了


京ICP备16022265号-2 Kotlin China 2017 - 2018