【发布时间】:2022-02-07 04:50:47
【问题描述】:
我在模块 A 中定义了这个数据类:
data class Amount(val value: Int)
并且我创建了以下扩展函数(也在模块 A 中):
inline fun <T> Iterable<T>.sumOf(selector: (T) -> Amount): Amount {
var sum = 0
for (element in this)
sum += selector(element).value
return Amount(sum)
}
示例用法如下所示:
val sum: Amount = (1..5).sumOf { Amount(it) }
当我在模块 A 中使用这个 sumOf 函数时,它工作得很好,但是当我尝试在不同的模块 B(它依赖于模块 A)中使用它时,编译器无法找到这个重载sumOf函数。
如果我将 sumOf 重命名为不同的名称(例如 sumBy 等),那么它也可以在模块 B 中使用。我注意到的另一件事是,即使我将该扩展函数放在模块 B 中,编译器也无法找到此重载。它显示的错误是:
None of the following functions can be called with the arguments supplied.
Iterable<TypeVariable(T)>.sumOf((TypeVariable(T)) → Int) where T = TypeVariable(T) for inline fun <T> Iterable<T>.sumOf(selector: (T) → Int): Int defined in kotlin.collections
followed by similar other overloads available for Long, ULong, etc.
这里有什么问题?
【问题讨论】:
标签: kotlin multi-module kotlin-extension