evant / kotlin-inject

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

Feature request - support deferring to superclass component properties #351

Open ZacSweers opened 4 months ago

ZacSweers commented 4 months ago

We have an example where we generate a component impl for a given class.

Scenario 1: declare parameter in the merge component, add property in impl

@MergeCircuitComponent(AppScope::class)
abstract class AppScopeCircuitComponent(@Component parent: AppComponent) : CircuitComponent

// Generated
abstract class AppScopeCircuitComponentImpl(@Component val parent: AppComponent) : AppScopeCircuitComponent(parent)

Scenario 2: declare property in the merge component, only add parameter in impl passed to parent

@MergeCircuitComponent(AppScope::class)
abstract class AppScopeCircuitComponent(@Component val parent: AppComponent) : CircuitComponent

// Generated
abstract class AppScopeCircuitComponentImpl(@Component parent: AppComponent) : AppScopeCircuitComponent(parent)

The latter feels preferably, but it seems only the former is supported. Curious if this is something that could be expanded or if there's a technical limitation.

Reference: https://github.com/slackhq/circuit/pull/1197

evant commented 4 months ago

Seems like it should be doable, we already scan superclasses for @Provides we could scan for @Component as well. Actually may be worth spending some time seeing if we can unify how they are looked up, I can't remember off the top of my head why they are different.

note: I'd actually expect scenario 2 to look like

@MergeCircuitComponent(AppScope::class)
abstract class AppScopeCircuitComponent(@Component val parent: AppComponent) : CircuitComponent

// Generated
abstract class AppScopeCircuitComponentImpl(parent: AppComponent) : AppScopeCircuitComponent(parent)
ZacSweers commented 4 months ago

Makes sense re: not having @Component in the generated component, I wasn't sure when I was writing this up 👍