Search code examples
androidkotlindagger-2

Understanding top level get() in Kotlin


While I was checking this repository about dagger in android with kotlin, I stumbled upon the application class:

class ConnectingTheDotsApp : Application() {
    val appComponent: AppComponent by lazy {
        DaggerAppComponent
            .factory()
            .create(this)
    }

    override fun onCreate() {
        super.onCreate()
        appComponent.inject(this)
    }
}

val Activity.appComponent get() = (application as ConnectingTheDotsApp).appComponent
val Fragment.appComponent get() = (requireActivity().application as ConnectingTheDotsApp).appComponent

The class I understand. But the two last lines of code, I cannot figure them out. I know that get() is a backing property but why is it outside a class and what about the Activity. and Fragment. what did they mean? Any Idea? Thanks


Solution

  • Don't mind guys. As IR42 stated, it's just a simple implementation of the extension properties in koltin.