https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
u

ursus

10/02/2020, 10:56 PM
Hi, with dagger multibinding, is there a way to default to empty set if non are found? Now it throws an error
g

gildor

10/04/2020, 12:24 AM
You can add an specify empty set by default
See official doc ; Section Declaring Multibindings) https://dagger.dev/dev-guide/multibindings.html
u

ursus

10/04/2020, 4:41 AM
Hmm I might be blind but not sure. Do you mean this?
Copy code
@Module
class MyEmptySetModule {
  @Provides @ElementsIntoSet
  static Set<Foo> primeEmptyFooSet() {
    return Collections.emptySet();
  }
}
g

gildor

10/04/2020, 4:53 AM
Yes
Or @Multibinds abstract Set<Foo> aSet();
It literally explains your case
Copy code
You do not have to use @Multibinds for sets or maps that have at least one @IntoSet, @ElementsIntoSet, or @IntoMap binding, but you do have to declare them if they may be empty.
u

ursus

10/04/2020, 1:16 PM
Yes but the issue is I dont really know if its going to be none ahead of time. Well I could but it requires thinking. The use case is monorepo. Now im building a new app which includes some modules out of which none put the moshi adapters into the graph. Sure I can place the empty set as in the sample to make it work, but then as the app grows I need the remember to remove the empty on first actual adapter include. But okay ill live with that. Thankd!
g

gildor

10/04/2020, 2:00 PM
No need to remember to remove it at all, it will not override existing bindigs, it just doesn't fail on compilation, so you say "it's fine if set is empty"
Also, it can be a part of module which requests this set, so module says "I do not require this set to be non-empty"
110 Views