generics - Kotlin泛型和TypeVariable

标签 generics kotlin type-variables

我想输入:

        project.tasks<JmhTask> {
            ...
        }

代替
        project.tasks.withType(JmhTask::class.java) {
            ...
        }
tasksTaskContainer类型,扩展了interface DomainObjectCollection<T> extends Collection<T>DomainObjectCollection还定义::withType如下:
<S extends T> DomainObjectCollection<S> withType(Class<S> type, Action<? super S> configureAction);
我尝试执行以下操作(为简单起见,首先针对TaskContainer接收器):
    inline operator fun <reified S> TaskContainer.invoke(configureAction: Action<in S>?) =
            withType(S::class.java, configureAction)

不幸的是withType标记为红色:
None of the following functions can be called with the arguments supplied.
withType(Class<TypeVariable(S)!>, (Closure<Any!>..Closure<*>))   where S = TypeVariable(S) for    fun <S : Task!> withType(type: Class<S!>, configureClosure: (Closure<Any!>..Closure<*>)): DomainObjectCollection<S!> defined in org.gradle.api.tasks.TaskContainer
withType(Class<TypeVariable(S)!>, Action<in TypeVariable(S)!>)   where S = TypeVariable(S) for    fun <S : Task!> withType(type: Class<S!>, configureAction: Action<in S!>): DomainObjectCollection<S!> defined in org.gradle.api.tasks.TaskContainer

我该如何解决?

最佳答案

这个怎么样

inline operator fun <reified S : Task> TaskContainer.invoke(configureAction: Action<in S>) =
    withType(S::class.java, configureAction)

关于generics - Kotlin泛型和TypeVariable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62193643/

相关文章:

android - kotlin 中的重载解析歧义错误

generics - 为什么编译器会将两个具有不同名称的等效签名的泛型类型变量识别为不同类型?

Haskell GADT 'Show' - 实例类型变量推导

具有通用函数参数的 Swift 数组扩展

generics - 如何在带有 React 的 Typescript/JSX 中使用带有箭头函数的泛型?

generics - 如何在 Swift 中返回一个序列?

Kotlin - 具有私有(private)构造函数的类的工厂函数

kotlin - 自定义 ETH 结算器实现过程中的问题

java - 类型变量如何允许类型错误?

c# - 将列表<B>转换到列表<A>