Kotlin 泛型和 TypeVariable

Kotlin generics and TypeVariable

我想输入:

        project.tasks<JmhTask> {
            ...
        }

相反

        project.tasks.withType(JmhTask::class.java) {
            ...
        }

tasks 属于 TaskContainer 类型,扩展了 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)