question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support multiple bound types for contributed bindings and multibindings

See original GitHub issue

Let’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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:18

github_iconTop GitHub Comments

7reactions
vRallevcommented, May 29, 2021

I thought about deprecating the boundType = KClass attribute in favor of boundTypes = [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 now boundType is the 2nd attribute, boundTypes would be at the 6th position). Second, the implicit behavior of ContributesBinding and ContributesMultibinding is 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:

The prototype is being worked on, and the feature is planned to be included under the -language-version 1.6 flag to Kotlin 1.5.30, and enabled by default since Kotlin 1.6.

That means we could start prototyping this solution relatively soon. My proposal for Anvil would be to make ContributesTo, ContributesBinding and ContributesMultibinding repeatable annotations.

5reactions
vRallevcommented, Nov 22, 2021

I’d like to give an update for this ticket. I’ve implemented repeatable annotations for @ContributesTo already. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found