https://kotlinlang.org logo
#dagger
Title
# dagger
d

David

10/27/2018, 6:59 PM
Hey @Sam - nah. It's a proper "high end" optimisation even for those Java users. I looked it up a while back and end up just doing the older style for my Kotlin modules and moved on. Be good to know if I was wrong on this point though? I think our time would be better spent turning things lazy if they need be and obvs not doing anything slow in the provisioning blocks.
Although looking back at my code I did use a companion object 🤔 ...I also remember thinking that this must be redundant!
s

Sam

10/27/2018, 7:05 PM
I tried few things, companion objects with @JvmStatic does help
Dagger generated code being in Java is using the static version (generated by Kotlin compiler)
and the DaggerAppComponent never creates an instance of the AppModule
It wouldn't make sense if Dagger generated code is going to be in kotlin
so far being in Java, seems to be fine
💪 1
d

David

10/27/2018, 7:08 PM
Ah nice! So if you are 💯 Kotlin then no effect but if you have Java then it remains valid?
Copy code
@Module
open class SyncModule {

    @Module
    companion object {
        /**
         * Provide access to the notification manager.
         *
         * @return the notification manager.
         */
        @Provides
        @JvmStatic fun providesNotificationManager(context: Context): NotificationManager {
            return context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
...
..snippet from one of my Kotlin modules
s

Sam

10/27/2018, 7:10 PM
even if you are in Kotlin it will be fine as long as Dagger generated code is in Java
In your case, look at the generated code of the component using the SyncModule
👍 1
it will be java based and will invoke providesNotificationManager as a static method on SyncModule
d

David

10/27/2018, 7:11 PM
I see. Will do after I've made dinner - post back your findings in the main channel!
s

Sam

10/27/2018, 7:12 PM
make sure to declare your SyncModule as abstract
there is really no reason to create an instance of it
and hence the optimization
d

David

10/27/2018, 7:14 PM
Ah so that code wouldn't of had a chance either way. Facepalm. 😅
Thanks for the tip!
s

Sam

10/27/2018, 7:15 PM
Sure
3 Views