Search code examples
kotlinkotlin-extension

this reference in a lazy initializer of kotlin extension property


I'm trying Kotlin and want to implement a lazy extension property for Activity:

/**
 * Activity module
 */
val Activity.activityModule: ActivityModule by lazy {
    ActivityModule(this)
}

The compiler errors with:

'this' is not defined in this context

How can I qualify this as Activity this? I have read a guide but can't get it. this@Activity says the reference is unresolved.


Solution

  • The lazy delegate in Kotlin doesn't have reference to a property member class.

    I see two solutions:

    1. transform it to extension function
    2. implement own delegate