简体   繁体   English

如何在Kotlin诗人中返回函数

[英]How to return function in kotlin poet

I'm using KotlinPoet for annotation processing, but I've got an issue. 我正在使用KotlinPoet进行注释处理,但是遇到了问题。 I need return function in generated class, but I'm not sure that KotlinPoet can do it? 我在生成的类中需要返回函数,但是我不确定KotlinPoet可以做到吗? If you have some experience, please help me. 如果您有经验,请帮助我。

That I can do: 我可以做的:

fun test(): String = ...

That I need to do 我需要做的

fun test(): () -> String = ...

ClassNameTypeName相似,可以使用LambdaTypeName创建不带参数且String返回类型的函数类型:

val type = LambdaTypeName.get(returnType = String::class.asTypeName())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何使用Kotlin Poet库生成类型安全的生成器函数 - Android - How to generate type safe builder function using Kotlin Poet library kotlin诗人中如何实现suspend关键字修改高阶函数 - How to implement the suspend keyword to modify higher-order functions in kotlin poet Kotlin 带类型参数的诗人超级接口 - Kotlin Poet super interface with type argument 如何从Kotlin函数类型返回 - How to return from Kotlin function type 如何从 Kotlin Android 中的 function 返回值 - How to return value from function in Kotlin Android 如何在Kotlin函数中返回变量对象 - How to return a variable object in kotlin function 如何使用布尔函数? Kotlin 中 if 语句中的返回类型 - how to use a function with Boolean? return type in an if statement in Kotlin Kotlin:如何将值从 lambda 返回到父 function? - Kotlin: How to return a value from a lambda to a parent function? 如何在视图模型中生成 function 中的字符串并返回到 Kotlin 中的片段? - How can I generate a String in a function in a viewmodel and return to a fragment in Kotlin? 如何从 kotlin 协程中的函数返回 fusedLocationProviderClient().lastLocation 作为流 - How to return fusedLocationProviderClient().lastLocation as flow from a function in kotlin coroutines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM