amzn / kotlin-inject-anvil

Extensions for the kotlin-inject dependency injection framework
Apache License 2.0
240 stars 4 forks source link

Subcomponents with parameters do not currently compile #17

Closed chrisbanes closed 2 months ago

chrisbanes commented 2 months ago

See the following example:

@ContributesSubcomponent
@ProfileScope
abstract class ProfileObjectGraph(
    @get:Provides override val profileId: ProfileId,
) {
    @ContributesSubcomponent.Factory
    @ApplicationScope
    interface Factory {
        fun createProfileObjectGraph(profileId: ProfileId): ProfileObjectGraph
    }
}

This fails to compile with the error due to No value passed for parameter 'profileId'. The generated code is:

@Component
@MergeComponent
@ProfileScope
@Origin(value = ProfileObjectGraph::class)
public abstract class ProfileObjectGraphFinalApplicationObjectGraph(
  @Component
  public val parentComponent: ApplicationObjectGraph,
  @get:Provides
  public val profileId: ProfileId,
) : ProfileObjectGraph, // <-- error is here, profileId isn't passed down
    ProfileObjectGraphFinalApplicationObjectGraphMerged {
  public interface Factory : ProfileObjectGraph.Factory {
    override fun createProfileObjectGraph(profileId: ProfileId): ProfileObjectGraph =
        ProfileObjectGraphFinalApplicationObjectGraph::class.create(this as ApplicationObjectGraph,
        profileId)
  }
}
chrisbanes commented 2 months ago

Ignore me. I missed the doc which says:

Parameters on the factory function are forwarded to the generated component and bound in the component

vRallev commented 2 months ago

The processor should check for this scenario and give you a better error message. That's a bug.

chrisbanes commented 2 months ago

There's probably another bug here, in that only interfaces are allowed for ContributesSubcomponents?

vRallev commented 2 months ago

Use there a good use case for an abstract class when the constructor parameters you had are provided through the factory?

chrisbanes commented 2 months ago

There probably isn't one, but it's worth documenting and/or throwing a better error in those instances?

vRallev commented 2 months ago

For sure!