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 keys within Map Multibindings

See original GitHub issue

It would be useful if when declaring a binding into a map, we could specify that one binding should be available under multiple keys.

Something like:

@Binds
@IntoMap
@TypeKey(Type.FOO)
@TypeKey(Type.BAR)
abstract Handler bindFooBarHandler(FooBarHandler handler);

or

@Binds
@IntoMap
@TypeKeys({Type.FOO, Type.BAR})
abstract Handler bindFooBarHandler(FooBarHandler handler);

Right now this can be accomplished with multiple @Binds methods within a module, which is a little verbose.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
ronshapirocommented, May 8, 2017

these annotations should have SOURCE retention

SOURCE retention will not always work if the module is in a different compilation from the @Component that installs the module. But this is a point, just wanted to correct the record.

0reactions
devindicommented, Feb 18, 2019

Hi guys,

I tried to use repeatable annotations from java 8 and I faced with issue.

Firstly I defined my custom annotation as map key like this (from tutorial):

enum MyEnum {
  ABC, DEF;
}

@MapKey
@interface MyEnumKey {
  MyEnum value();
}

After that I tried to bind same object to a few keys:

@Provides @IntoMap
  @MyEnumKey(MyEnum.ABC)
  @MyEnumKey(MyEnum.DEF)
  static String provideAbcOrDefValue() {
    return "value for ABC or DEF";
  }

Compiler said me that I should mark my annotation with Repeatable one This annotation this available since API level 24 so I can’t use multiple keys in my project.

Even in 2k19 we should support devices with API level 19 or even API level 17 so it would be great to support multiple keys from dagger’s side.

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 ... Keys must be unique, and any collision within...
Read more >
Dagger 2 Multibindings Illustrated | by Elye - Medium
Dagger allows you to bind several objects into a collection even when ... Dagger 2 Multibinds into Map can only work with case...
Read more >
Dagger by Tutorials, Chapter 14: Multibinding With Maps
In this chapter, you'll learn how to use multibinding with Map. ... You'll also create a simple custom key and use @KeyMap to...
Read more >
Dagger2 Map multibinding : What do I provide for this java.util ...
You should inject the constructer of the viewModel class Example: @Inject public AuthViewModel(){} It should work fine.
Read more >
Understanding Dagger 2 Multibindings + ViewModel
says roughly: “inject this object into a Map ( @IntoMap annotation) using UserViewModel.class as key, and a Provider that will build a ...
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