我想生成一个带有typealias的kotlin类定义。
typealias MyAlias = BigDecimal
class TemplateState(var test: MyAlias) {
}有什么建议吗?
发布于 2020-01-27 18:43:08
您可以在documentation中找到它
//create a TypeAlias and store it to use the name later
val typeAlias = TypeAliasSpec.builder("MyAlias", BigDecimal::class).build()
val type = TypeSpec.classBuilder("TemplateState").primaryConstructor(
FunSpec.constructorBuilder().addParameter(
//You can use the ClassName class to get the typeAlias type
ParameterSpec.builder("test", ClassName("", typeAlias.name)).build()
)
).build()
FileSpec.builder("com.example", "HelloWorld")
.addTypeAlias(typeAlias)
.addType(type)
.build()发布于 2020-01-28 21:09:25
KotlinPoet实际上并不关心ClassName代表的是typealias还是真正的类型。在您的示例中,ClassName("", "MyAlias") (假设在默认包中声明了MyAlias )就足以用作构造函数参数的类型。当然,您需要单独生成typealias以确保生成的代码可以编译。
https://stackoverflow.com/questions/59929368
复制相似问题