英文:
Why do lines 14 and 21 not compile (for my Kotlin function)?
问题
我有一个名为collectCustomizerFunctions
的函数,它应该创建一个MutableList<KCallable<*>>
,其中包含指定类及其子类中用CustomizerFunction
注解的所有函数。
customizerFuns
(MutableList<KCallable<*>>
)应该递归地将所有“cutomizer functions”添加到其中。
当我尝试构建我的Gradle项目时,出现了两个异常:
e: collectCustomizerFuns.kt:14:33 类型推断失败。类型参数T的值应在输入类型(参数类型、接收器类型或预期类型)中提及。请尝试明确指定它。
e: collectCustomizerFuns.kt:21:30 类型不匹配:推断的类型为Any,但预期的是CapturedType(*)。
以下是我的代码:
3 | import kotlin.reflect.KClass
4 | import kotlin.reflect.KCallable
5 | import kotlin.reflect.full.allSuperclasses
6 |
7 | @Utility
8 | public tailrec fun <T: Any> collectCustomizerFuns(
9 | specClass: KClass<T>,
10 | customizerFuns: MutableList<KCallable<*>>
11 | ): Unit {
12 | // 添加此类的带注解函数
13 | for (member in specClass.members) {
14 | if (CustomizerFunction::class in member.annotations) { <--- 错误
15 | customizerFuns.add(member)
16 | } else {}
17 | }
18 |
19 | // 添加所有超类的带注解函数
20 | for (superclass in specClass.allSuperclasses) {
21 | collectCustomizerFuns<Any>(superclass, customizerFuns) <--- 错误
22 | }
23 | }
我已经尝试修复这些错误一段时间了,如果能提供帮助,将不胜感激!
另外,请提供任何关于这个函数的建设性批评,这将非常有帮助!
英文:
I have a function named collectCustomizerFunctions
which should create a MutableList<KCallable<*>>
of all the functions of a specified class and its sub-classes which are annotated with CustomizerFunction
.
Recursively, customizerFuns
(the MutableList<KCallable<*>>
) should have all of the "cutomizer functions" added to it.
When I try to build my Gradle project, it fails with two exceptions:
e: collectCustomizerFuns.kt:14:33 Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
e: collectCustomizerFuns.kt:21:30 Type mismatch: inferred type is Any but CapturedType(*) was expected
Here is my code:
3 | import kotlin.reflect.KClass
4 | import kotlin.reflect.KCallable
5 | import kotlin.reflect.full.allSuperclasses
6 |
7 | @Utility
8 | public tailrec fun <T: Any> collectCustomizerFuns(
9 | specClass: KClass<T>,
10 | customizerFuns: MutableList<KCallable<*>>
11 | ): Unit {
12 | // add annotated functions of this class
13 | for (member in specClass.members) {
14 | if (CustomizerFunction::class in member.annotations) { <--- ERROR
15 | customizerFuns.add(member)
16 | } else {}
17 | }
18 |
19 | // add annotated functions of all super-classes
20 | for (superclass in specClass.allSuperclasses) {
21 | collectCustomizerFuns<Any>(superclass, customizerFuns) <--- ERROR
22 | }
23 | }
I have been trying to fix these bugs for a while now, and would appreciate any help!
Also, please provide any constructive criticism you want regarding this function, it would help a lot!
答案1
得分: 1
关于第一个错误,member.annotations
返回的是 List<Annotation>
。你需要获取这些注解的实际类。
关于第二个错误,在调用 collectCustomizerFuns
时去掉类型,让 Kotlin 自动推断类型 :).
所以尝试这样做:
public tailrec fun <T: Any> collectCustomizerFuns(
specClass: KClass<T>,
customizerFuns: MutableList<KCallable<*>>
) {
// 添加此类的带注解的函数
for (member in specClass.members) {
if (CustomizerFunction::class in member.annotations.map { it.annotationClass }) {
customizerFuns.add(member)
} else {
}
}
// 添加所有超类的带注解的函数
for (superclass in specClass.allSuperclasses ) {
collectCustomizerFuns(superclass, customizerFuns)
}
}
顺便说一句,你可以从方法签名中移除 Unit
。
英文:
For the first error, member.annotations
returns List<Annotation>
. You have to fetch actual classes of these annotations.
For the second error, remove the type where you call collectCustomizerFuns
. Let kotlin infers the type by itself :).
So try this:
public tailrec fun <T: Any> collectCustomizerFuns(
specClass: KClass<T>,
customizerFuns: MutableList<KCallable<*>>
) {
// add annotated functions of this class
for (member in specClass.members) {
if (CustomizerFunction::class in member.annotations.map { it.annotationClass }) {
customizerFuns.add(member)
} else {
}
}
// add annotated functions of all super-classes
for (superclass in specClass.allSuperclasses ) {
collectCustomizerFuns(superclass, customizerFuns)
}
}
By the way, you can remove Unit
from the method signature.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论