https://kotlinlang.org logo
Title
j

jlleitschuh

11/28/2017, 5:24 PM
How do I prevent these two declarations from having the same JVM type signature?
class NamedDomainObjectContainerScope<T : Any>(
    private val container: NamedDomainObjectContainer<T>) : NamedDomainObjectContainer<T> by container {

    operator fun String.invoke(configuration: T.() -> Unit): T =
        this().apply(configuration)

    /**
     * @see [NamedDomainObjectContainer.maybeCreate]
     */
    inline
    operator fun <reified U : T> String.invoke(configuration: U.() -> Unit): U =
        this(U::class).apply(configuration)
}
Use case: I want to be able to do this:
container { // `this` is type `NamedDomainObjectContainerScope<Task>`
    "allice" { /* Type Task in this scope*/ }
    "bob"<ExtendedTaskType> { /* Type ExtendedTaskType in this scope*/ }
}
My more general problem is that I'm trying to add
reified
overloads for all of these methods in the Gradle Kotlin DSL: https://github.com/gradle/kotlin-dsl/blob/6a3c51a65e8423aa33432b816996360cd32e7906/provider/src/main/kotlin/org/gradle/kotlin/dsl/NamedDomainObjectContainerExtensions.kt
a

Andreas Sinz

11/28/2017, 5:31 PM
@jlleitschuh what problem are you facing? declarations clash?
j

jlleitschuh

11/28/2017, 5:32 PM
Yea. And if I just have the reified version then the invoke the compiler requires an explicit generic at the call site which I don't want.
a

Andreas Sinz

11/28/2017, 5:34 PM
j

jlleitschuh

11/28/2017, 5:35 PM
Oooh. Clever. Hopefully it doesn't get slapped down when it comes to the code review. I'll give that a shot though.
This solved the problem nicely!!! Thanks!!!
👍 2