https://kotlinlang.org logo
Title
c

Carsten L

11/24/2017, 9:33 AM
Hey everyone. Maybe someone could give a little help. What is the right way to use
DomainObjectCollection.withType
in Kotlin DSL? my example when writing a Plugin :
project.run {
    repositories.withType(MavenArtifactRepository::class.java) {
        // I want to access the typed repo
        dostuff( this)
    }
}
what Building gives me: Error:(22, 44) Kotlin: Type mismatch: inferred type is Project but MavenArtifactRepository was expected I tried
this@withType
but that just yields: ‘this’ is not defined in this context. Can anyone hint me how to do this?
c

Czar

11/24/2017, 10:14 AM
try like this:
project.run {
	repositories.withType<MavenArtifactRepository> {
		doStuffTo(this)
	}
}
with kotlin try to avoid using
::class.java
when a reified generic is available, like in this instance. Generic looks far cleaner and gives you type inference.
c

Carsten L

11/24/2017, 10:57 AM
Argh. Haven’t noticed this. 😖 Thank you! 🙂
😉 1