懒惰的kotlin扩展属性初始化程序中的此引用 [英] this reference in a lazy initializer of kotlin extension property

查看:54
本文介绍了懒惰的kotlin扩展属性初始化程序中的此引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Kotlin,并想为Activity实现一个惰性扩展属性:

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

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

编译器出现以下错误:

'this' is not defined in this context

我怎样才能将此列为活动"?我已阅读指南,但无法获取. this @ Activity 说引用未解决.

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.

推荐答案

Kotlin中的 lazy 委托没有引用属性成员类.

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

我看到了两种解决方案:

I see two solutions:

  1. 将其转换为扩展功能
  2. 实现自己的委托

这篇关于懒惰的kotlin扩展属性初始化程序中的此引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆