Comments (6)
Using a companion object
is a perfectly fine pattern in Kotlin to write a Dagger module that combines @Binds
and @Provides
declarations. You must realize that the companion object
is a different class (an object
class) from the enclosing one. In other words you are applying the same pattern as in FAQs page that was written for Java but for Kotlin.
from dagger.
Yes a companion object makes a singleton object
behind the scenes with your declarations. We all know that. That isn't really the point. I have been using Dagger since literally Dagger1 and there has never in my memory been an instance of an implied module. It is exactly because it is a separate class that is so confusing. I would maybe understand if it instead relied on @JvmStatic, but that isn't even the case here. It also confuses me because it is way less boilerplate than the interface Declarations {
. So, why bother documenting the Declarations object way of doing it if an easier solution existed. My only thought is this is somewhat new and undocumented or maybe an accident that people just went with?
from dagger.
The solution in the FAQs page is for Java and pre-dates the wide adoption of Kotlin. The support for binding declaration in Kotlin companion objects was added in 2.26. Maybe the action item here is to update the dagger.dev site.
from dagger.
It's separate, sure, but critically it's also associated with the enclosing class. Hence the companion
modifier. If you drop that and merely use a nested object
this behavior will not work. Traversing the association to discover instance-less (kinda) providers is an ergonomic improvement for Kotlin language support.
from dagger.
I am not arguing that it isn't ergonomic. I'm not arguing anything at all lol. I just have never seen this before and couldn't find documentation on it. I didn't know if I changed all my interface Declarations {
to companion objects, would this feature break or get removed later.
My understanding now is that this is a feature added in 2.26 that can be used and will not be removed. It is just as good (if not better) than the interface Declarations {
and if nothing else a lot shorter to write. Thank you.
from dagger.
Thanks for letting us know about the missing documentation! I added this to dagger.dev
from dagger.
Related Issues (20)
- error: [Hilt] Expected @AndroidEntryPoint to have a value HOT 3
- @IntoSet analog for injectable classes HOT 3
- Hilt Generated Code having not used code? HOT 2
- [Dagger-Hilt] Can't get viewModel in composable by Hilt HOT 1
- Update Dagger guava dependency to address CVE-2023-2976 HOT 2
- Add KMP support for @HiltViewModel annotation HOT 2
- Dagger resolution and dynamic feature module HOT 1
- Feature request: Manually regenerate application component HOT 1
- Using TestContainer.com HOT 2
- Detect dependency cycles containing Lazy or Provider during the compilation time HOT 6
- error when ksp.useKSP2=true HOT 2
- Suppress annotation on @Inject is not honored on the Factory HOT 1
- [KSP2] Dagger and Hilt incompatibility with KSP2 HOT 2
- [KSP2] NullPointerException: null cannot be cast to non-null type androidx.room.compiler.processing.XType HOT 1
- App crashing with NPE for some classes when Lazy Class Key is used HOT 4
- OutOfMemoryError with Maven HOT 1
- DaggerAppComponent not creating in the version 2.51.1 HOT 3
- java.lang.ClassNotFoundException: dagger.spi.shaded.androidx.room.compiler.processing.javac.JavacBasicAnnotationProcessor. Hilt 2.49+ HOT 1
- how to manually generate module HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dagger.