为什么作业不是陈述

时间:2016-02-24 12:35:07

标签: kotlin

我有以下代码:

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类型的语句?

1 个答案:

答案 0 :(得分:1)

无论我们喜不喜欢,这只是语言创作者所做出的设计决定。有关详细信息,请参阅此讨论:

https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339

相关问题