Support multiple bound types for contributed bindings and multibindings
See original GitHub issueLet’s say I have two interfaces A and B and class implements both:
class C @Inject constructor(
......
): A, B {
}
to bind it I can add to dagger module:
@Binds
@IntoSet
abstract fun bindA(c: C): A
@Binds
@IntoSet
abstract fun bindB(c: C): B
As I understand, currently I can bind it only to one of the interfaces via @ContributesMultibinding e.g. @ContributesMultibinding(AppScope::class, A::class) (and binding to B is still in the dagger module).
Is there a better solution?
Thanks in advance!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:18
Top Results From Across the Web
Multibindings - Dagger
Dagger allows you to bind several objects into a collection even when the objects are bound in different modules using multibindings. Dagger assembles...
Read more >com.google.inject.multibindings Documentation Differences
Contributing mapbindings from different modules is supported. ... When multiple equal keys are bound, the value that gets included in the map is...
Read more >Guice - How to let multiple multiple modules contribute to one ...
Basically using bindings (or multibindings, for that matter) across different modules. How could I accomplish that and which approach would be ...
Read more >Multibindings - GitHub
Multibinder Multibindings make it easy to support plugins in your application. ... way contributes to the existing Set of implementations for that type....
Read more >Silverlight MultiBindings, How to attached multiple bindings to ...
Therefore, in order to allow binding on the MultiBinding class, it must be a FrameworkElement, this gives us the DataContext property and the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I thought about deprecating the
boundType = KClassattribute in favor ofboundTypes = [KClass], but I don’t like this approach for two reasons. First, the new attribute would need to be added to the end of the attribute list for source compatibility. The new ordering of the attributes would be strange (right nowboundTypeis the 2nd attribute,boundTypeswould be at the 6th position). Second, the implicit behavior ofContributesBindingandContributesMultibindingis that if there’s one single super type, then it serves as the default bound type. It’s awkward to allow declaring multiple bound types manually, but not respecting multiple super types implicitly. I won’t move forward with this option.The other solution is to allow repeated annotations. However, Kotlin only supports source retention for repeated annotations, but Anvil requires runtime retention to read all annotations from binaries (actually binary retention would be enough, but we decided for source retention to leave the door open for interesting options). So we can’t use repeated annotations today.
Kotlin 1.6 will support repeated annotation with a binary and source retention. The ticket is KT-12794 and the Keep is available here. For the timeline they mention:
That means we could start prototyping this solution relatively soon. My proposal for Anvil would be to make
ContributesTo,ContributesBindingandContributesMultibindingrepeatable annotations.I’d like to give an update for this ticket. I’ve implemented repeatable annotations for
@ContributesToalready. It’ll be a big change for the Anvil internals, but it’s definitely very convenient on the consumers side.The problem is that Kotlin 1.6.0 shipped with significant bugs, e.g. incremental compilation is broken. We’ll ship Anvil versions for 1.5 and 1.6 for a longer period of time. But this also means that annotations can’t be repeatable, yet.