elect
elect

Reputation: 7190

Kotlin generics and TypeVariable

I'd like to type:

        project.tasks<JmhTask> {
            ...
        }

Instead

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

tasks is of TaskContainer type, which extends interface DomainObjectCollection<T> extends Collection<T>

DomainObjectCollection also defines ::withType as follows:

<S extends T> DomainObjectCollection<S> withType(Class<S> type, Action<? super S> configureAction);

I tried to do the following (first against TaskContainer receiver for simplicity):

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

Unfortunately withType is marked red:

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

How can I solve it?

Upvotes: 1

Views: 3121

Answers (1)

goedi
goedi

Reputation: 2083

How about this

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

Upvotes: 5

Related Questions