簡體   English   中英

為什么作業不是陳述

[英]why assignments are not statements

我有以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM