Kotlin-dsl-samples: tasks.withType type parameter constraint is too restrictive

Created on 25 Oct 2017  路  4Comments  路  Source: gradle/kotlin-dsl-samples

I can't configure all verification tasks using withType function in the following way:

tasks.withType<VerificationTask> {
    ignoreFailures = false
}

The VerificationTask is an interface that does not extend Task.

withType is declared as following:

fun <reified S : Any> DomainObjectCollection<in S>.withType(crossinline configuration: S.() -> Unit)

TaskContainer is a DomainObjectCollection<Task>. Therefore only Task and its subtypes can be used as S.

Your Environment

kotlin-dsl embedded in Gradle 4.2

feature gradle-api ease-of-use groovy-parity

Most helpful comment

All 4 comments

I believe in S projection is not appropriate here, because the function doesn't put anything _into_ the collection.

Another unfortunate thing is, I think in Groovy it just kind of works because Groovy throws out generic information.

tasks.withType(VerificationTask) {
   ignoreFailures = false
}

I haven't tried that, but I think it actually works.

Other types that also impacts are:

Closing in favor of upstream https://github.com/gradle/gradle/issues/4050

Was this page helpful?
0 / 5 - 0 ratings