Why appointments are not statements

I have the following code:

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
}

I know that I can simply write:

class Presenter {
    var view : View? = null
}

and then in the code just call presenter.view = View()and presenter.view = nullinstead of attachView/ detachView. But I think this is much less readable.

So why can't I use assignments as expression bodies in Kotlin? Why is assignment not just a type expression Unit?

+4
source share
1 answer

Whether we like it or not, it was just a design decision created by the creators of the language. See the discussion for more details:

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

+1
source

All Articles