evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.14k stars 51 forks source link

Names clash for properties in component with multiple super types #369

Closed vRallev closed 2 months ago

vRallev commented 3 months ago

Assume following component, which compiles fine

interface Component1 {
    val string: String
}

@Component
abstract class TestComponent : Component1 {
    @Provides
    fun provideString(): String = ""
}

If I add another interface as super type containing a property with the same name and type, then kotlin-inject throws an error instead of deduplicating the properties:

interface Component1 {
    val string: String
}

interface Component2 {
    val string: String
}

@Component
abstract class TestComponent : Component1, Component2 {
    @Provides
    fun provideString(): String = ""
}

Results in:

e: file:///InjectTestComponent.kt:13:16 Conflicting declarations: public open val string: String, public open val string: String
e: file:///InjectTestComponent.kt:16:16 Conflicting declarations: public open val string: String, public open val string: String

kotlin-inject should deduplicate the properties. In a large enough modularized codebase this comes up more often than not.

In a step further, kotlin-inject should inspect super types and use the most concrete type for the property, e.g. following should be supported too:

interface Component1 {
    val string: String
}

interface Component2 {
    val string: CharSequence
}

@Component
abstract class TestComponent : Component1, Component2 {
    @Provides
    fun provideString(): String = ""
    @Provides
    fun provideCharSequence(string: String): CharSequence = string
}
vRallev commented 2 months ago

I realized that this is fixed in the latest snapshot builds, likely 16bc1bcb375538c9c9b6c1e9d28456d0a5d8c00c It would be okay for me to close the issue.

evant commented 2 months ago

Yes, good to know!