Coder Social home page Coder Social logo

Comments (6)

danysantiago avatar danysantiago commented on June 3, 2024

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.

dmstocking avatar dmstocking commented on June 3, 2024

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.

danysantiago avatar danysantiago commented on June 3, 2024

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.

JakeWharton avatar JakeWharton commented on June 3, 2024

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.

dmstocking avatar dmstocking commented on June 3, 2024

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.

Chang-Eric avatar Chang-Eric commented on June 3, 2024

Thanks for letting us know about the missing documentation! I added this to dagger.dev

from dagger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.