未实现代码的Kotlin存根/占位符函数

Scala的Predef有一堆速记功能,其中一个用于未实现的代码,即???。 Kotlin有什么相似之处,而不是不得不求助于慢速

throw Error("Code not yet implemented") 

更新现在有Kotlin标准库中的TODO函数 ,该函数接受一个可选消息。

不,Kotlin目前没有语法来表示未实现的代码段。 如果您觉得在项目中会使用这个功能,那么您可以声明一个返回类型为Nothing的函数,这样可以使控制流分析在您调用它的任何地方都很开心:

 fun notImplemented(): Nothing = throw Error("Code not yet implemented") ... ... fun foo(): String = notImplemented() // OK ... fun bar(s: String?): Int { if (s == null) notImplemented() return s.length() // Also OK, s is non-null here } 

此外,您可以使用标准的函数error ,该错误消息将引发一个IllegalStateException与该消息。