How does Android Studio Compiler generate method name for var editText by mutableStateOf("") automatically?

huangapple go评论64阅读模式
英文:

How does Android Studio Compiler generate method name for var editText by mutableStateOf("") automatically?

问题

When I compile Code A, I get an error, the system prompts me that there exists a method named setEditText.

But fun setIsShowEditDialog(isShow:Boolean) is OK without prompting the existence of a method named setIsShowEditDialog, why?

I have to change fun setEditText(input:String) to fun set_EditText(input:String), and it's OK to compile.

But Android Studio prompts me a warning message: "Function name 'set_EditText' should not contain underscores," why?

Code A

class EditDialogState private constructor(context: Context) {

    var isShowEditDialog by mutableStateOf(false)
        private set

    var editText by mutableStateOf("")
        private set

    fun setIsShowEditDialog(isShow:Boolean) {    // It's OK
        isShowEditDialog = isShow
    }

    fun set_EditText(input:String) {             // No compile error
        editText = input
    }
}
英文:

When I compile Code A, I get an error, the system prompt me exists a method name setEditText.

But fun setIsShowEditDialog(isShow:Boolean) is OK without prompt exists a method name setIsShowEditDialog, why?

I have to change fun setEditText(input:String) to fun set_EditText(input:String), it's OK to compile.

But Android Studio prompt me a warning information: Function name 'set_EditText' should not contain underscores , why ?

Code A

class EditDialogState private constructor(context: Context) {

    var isShowEditDialog by mutableStateOf(false)
        private set

    var editText by  mutableStateOf("")
        private  set

    fun setIsShowEditDialog(isShow:Boolean) {    //It's OK
        isShowEditDialog = isShow
    }

    fun setEditText(input:String) {             //Compile Error
        editText = input
    }
}

答案1

得分: 4

以下是您提供的代码的翻译部分:

答案在 Kotlin 属性 setter 和 getter 方法名称的实现细节中。

class MyTemp {
    var showEditDialog by mutableStateOf(false)
        private set

    var isNewShowEditDialog by mutableStateOf(false)
        private set

    // 会导致错误
    fun getShowEditDialog(): Boolean {
        return true
    }

    // 会导致错误
    fun setShowEditDialog(isShow: Boolean) {}

    // 会导致错误
    fun isNewShowEditDialog(): Boolean {
        return true
    }

    // 会导致错误
    fun setNewShowEditDialog(isShow: Boolean) {}
}

我们可以看到对具有前缀 "is" 的变量进行了特殊处理。
这导致您观察到的差异。
这也适用于其他类型,例如字符串。

英文:

The answer is in the implementation details of Kotlin property setter and getter method names.

class MyTemp {
    var showEditDialog by mutableStateOf(false)
        private set

    var isNewShowEditDialog by mutableStateOf(false)
        private set

    // Will give error
    fun getShowEditDialog(): Boolean {
        return true
    }

    // Will give error
    fun setShowEditDialog(isShow: Boolean) {}

    // Will give error
    fun isNewShowEditDialog(): Boolean {
        return true
    }

    // Will give error
    fun setNewShowEditDialog(isShow: Boolean) {}
}

We can see special handling for variables with the prefix "is".
This causes the difference you are observing.
This is applicable to other types as well like strings.

huangapple
  • 本文由 发表于 2023年6月29日 16:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定