【发布时间】:2017-05-25 13:34:26
【问题描述】:
我有以下简单的 Kotlin 扩展函数:
// Get the views of ViewGroup
inline val ViewGroup.views: List<View>
get() = (0..childCount - 1).map { getChildAt(it) }
// Get the views of ViewGroup of given type
inline fun <reified T : View> ViewGroup.getViewsOfType() : List<T> {
return this.views.filterIsInstance<T>()
}
这段代码可以编译并且运行良好。但是,我希望函数getViewsOfType 成为一个属性,就像views 一样。 Android Studio 甚至建议这样做。我让 AS 进行重构并生成以下代码:
inline val <reified T : View> ViewGroup.viewsOfType: List<T>
get() = this.views.filterIsInstance<T>()
但是这段代码无法编译。它会导致错误:“属性的类型参数必须在其接收者类型中使用”
这里有什么问题?搜索有关此错误的帮助似乎无法找到答案。
【问题讨论】: