Short answer is no, because for Anvil a “scope” on...
# squarelibraries
r
Short answer is no, because for Anvil a “scope” only needs to be a class reference. It doesn’t need to be a Dagger
@Scope
. For example our scope annotation looks like that:
Copy code
@Scope
annotation class SingleIn(val value: KClass<*>)
The usage side looks similar to this:
Copy code
@SingleIn(AppScope::class)
@ContributesTo(AppScope::class)
@MergeComponent(AppScope::class)
Anvil wouldn’t be able to make the connection that
@SingleIn
is the actual Dagger scope.
d
But you can't have:
Copy code
@Scope
@AnvilScope
annotation class AppScope
? I mean, that in a bunch of cases it just happens to be that all the classes are scoped in a certain sub/component... so that would save specifying the scope to Anvil... the only thing is that
@ContributesTo()
is a bit funny 😅!
r
You can do this. I don’t know what you mean with
@AnvilScope
though. With
Copy code
@Scope annotation class AppScope`
you would write
Copy code
@AppScope
@ContributesBinding(AppScope::class)
class YourClass @Inject constructor(..) : Abc
or
Copy code
@ContributesTo(AppScope::class)
@Module
object YourDaggerModule
and
Copy code
@MergeComponent(AppScope::class)
@AppScope
interface YourComponent
d
Ok, thanks, that's certainly better... I guess documentation is one of the hardest things, but it would really be nice if there was more elaborate docs on each subject with examples that represent best practices... I especially found subcomponents lacking in docs recently, this also might be a good thing to add.
@ralf I'm REALLY having troubles with subcomponents... what's the minimum setup for them to work with Anvil?
Or should I just do it the regular way like in Dagger2? There's no simpler way that generates all the necessary things? Also, in my subcomponent's module, I need to pass in something in the module's constructor...
r
There’s nothing special about subcomponents compared to vanilla Dagger subcomponents. You use
@MergeSubcomponent
and then can contribute bindings and Dagger modules to this scope.
d
What about the boilerplate of adding the subcomponent to the component, Anvil doesn't take care of that?
I didn't fully understand
@ContributesSubcomponent
, but I thought it did that? Also, when using it, it mentions using some kind of Factory annotation from Anvil instead of the dagger one, so it doesn't save the boilerplate?
106 Views