为什么分配不是陈述
我有以下代码:
class Presenter { private var view : View? = null fun attachView(view: View) = this.view = view // error: Assignment is not a statement fun detachView() = view = null // error: Assignment is not a statement }
我知道我可以写:
class Presenter { var view : View? = null }
稍后在代码中调用presenter.view = View()
和presenter.view = null
而不是attachView
/ detachView
。 但是我认为这是不太可读的。
那么为什么我不能在Kotlin中使用任务作为表达体呢? 为什么不分配只是Unit
类型的声明?
无论我们喜不喜欢,这只是语言创造者所做的设计决定。 看到这个讨论更多的细节:
https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339
- 如何在Kotlin html框架中表示Web组件标签?
- 如何在kotlin编程语言中使用如下所述的循环?
- Kotlin数据类中的可变集合的防御副本
- GridView只在单击项目时删除第一个项目。 基于Java>Kotlin
- 在Kotlin中传递listOf(mapOf(x,y,z))作为参数
- onCreate在抽象的父活动no kotlin中调用
- 如何从字符串转换为Kotlin类的接口/类?
- 如何使用自定义的类对象在android中的onCreate()中使用正确实现lateinit避免“属性获取或设置器预期”错误
- Kotlin中的Wicket NumberTextField在提交时抛出ClassCastException