kotlin - 如何使用 FunSpec.overriding?在 Kotlin 诗人
问题描述
由Fleshgrinder在GitHub 上提交。
该类FunSpec
具有非常方便的覆盖方法,但是,尚不清楚在生成代码时如何使用它。
最小的例子:
FileSpec.builder("com.fleshgrinder", "KotlinPoet").apply {
val className = ClassName("com.fleshgrinder", "KotlinPoet")
addType(TypeSpec.classBuilder(className).apply {
addFunction(FunSpec.builder("toString").apply {
addModifiers(KModifier.OVERRIDE)
addStatement("""return "KotlinPoet"""")
}.build())
}.build())
}.build().writeTo(System.out)
生成:
class KotlinPoet {
override fun toString() = "KotlinPoet"
}
它生成的输出是完美的,但生成它的代码却不是。
FunSpec.overriding(Any::toString).apply {
addStatement("""return "KotlinPoet"""")
}.build()
解决方案
推荐阅读
- javascript - 我如何路由到指定的 ng-include 模板
- python - 当我在一个表上创建关系并从该表中获取两列时出现问题
- redis - 为什么使用 RabiitMQ 作为代理和 redis 作为结果后端?
- gnuplot - 我的 gnuplot 直方图未标准化为 1,而是标准化为其他值。为什么?
- xml - 将 XML 映射到 Excel 中的单个单元格而不是列
- javascript - flat @import [url] css 资源到被调用的 css 文件中
- qt - 如何在qt中从右侧创建导航抽屉
- python - 计算时间列值变化
- git - 将 react 客户端推送到 repo
- c++ - 什么是编译器诊断?