amzn/kotlin-inject-anvil

Subcomponents with parameters do not currently compile

Closed this issue · 6 comments

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)
  }
}

Ignore me. I missed the doc which says:

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

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

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

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

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

For sure!